C#程序默认管理员启动
来源:互联网 发布:天音网络发展有限公司 编辑:程序博客网 时间:2024/06/06 00:50
static void Main(string[] Args)
{
try
{
//获得当前登录的Windows用户标示
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
//创建Windows用户主题
Application.EnableVisualStyles();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
//如果是管理员,则直接运行
Application.EnableVisualStyles();
Application.Run(new frmMain());
}
else
{
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//设置运行文件
startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
//设置启动参数
startInfo.Arguments = String.Join(" ", Args);
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "runas";
//如果不是管理员,则启动UAC
System.Diagnostics.Process.Start(startInfo);
//退出
System.Windows.Forms.Application.Exit();
}
//Application.Run(new frmMain());
}
catch {
Application.Restart();
}
{
try
{
//获得当前登录的Windows用户标示
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
//创建Windows用户主题
Application.EnableVisualStyles();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
//如果是管理员,则直接运行
Application.EnableVisualStyles();
Application.Run(new frmMain());
}
else
{
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//设置运行文件
startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
//设置启动参数
startInfo.Arguments = String.Join(" ", Args);
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "runas";
//如果不是管理员,则启动UAC
System.Diagnostics.Process.Start(startInfo);
//退出
System.Windows.Forms.Application.Exit();
}
//Application.Run(new frmMain());
}
catch {
Application.Restart();
}
}
替换点Main
阅读全文
0 0
- C#程序默认管理员启动
- C#默认以管理员身份运行程序
- C#默认以管理员身份运行程序
- C#默认以管理员身份运行程序
- C#默认以管理员身份运行程序
- C# 让程序默认以管理员运行
- C#默认以管理员身份运行程序
- C#默认以管理员身份运行程序
- vs2003生成的程序,启动默认以管理员身份运行
- [Winform]默认以管理员身份运行程序
- 在VS.net(C#)里,设置程序在启动时默认"以管理员身份运行"的方法
- C# 使用管理员身份启动应用程序源码
- 以Vista下管理员身份启动程序
- 绕过UAC以管理员身份启动程序
- win7设置让应用程序默认以管理员开机启动
- c# 程序以管理员身份运行
- c#程序以管理员身份运行
- C#如何以管理员身份运行程序
- JAVA作业题
- poj1780(模拟+回溯)
- Android系统启动过程
- TCP/IP协议族【第15章 传输控制协议(TCP)】
- 在C#源码中使用WebSocket服务发送消息
- C#程序默认管理员启动
- VC++直接操作使用C#类库基本操作
- plsql developer如何查询SQL语句执行历史记录(转)
- JSON字符串转换为Map
- Redis的集群结构
- HomeBrew 安装 mysql
- powershell的时间操作 取到某个文件里的时间值后加一天覆盖原文件的值
- 微服务架构上云最佳实践
- [目标检测]SSD: Single Shot MultiBox Detector