C#默认以管理员身份运行程序
来源:互联网 发布:菊池武夫 知乎 编辑:程序博客网 时间:2024/05/19 17:58
static void Main(string[] Args)
{
/**
* 当前用户是管理员的时候,直接启动应用程序
* 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
*/
//获得当前登录的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 Form1());
}
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();
}
}
{
/**
* 当前用户是管理员的时候,直接启动应用程序
* 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
*/
//获得当前登录的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 Form1());
}
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();
}
}
0 0
- C#默认以管理员身份运行程序
- C#默认以管理员身份运行程序
- C#默认以管理员身份运行程序
- C#默认以管理员身份运行程序
- C#默认以管理员身份运行程序
- C#默认以管理员身份运行程序
- [Winform]默认以管理员身份运行程序
- c# 程序以管理员身份运行
- c#程序以管理员身份运行
- C#如何以管理员身份运行程序
- C#如何以管理员身份运行程序
- C#如何以管理员身份运行程序
- C#如何以管理员身份运行程序
- C#如何以管理员身份运行程序
- C#如何以管理员身份运行程序
- win7下让程序默认以管理员身份运行
- Winform程序在win7中默认以管理员身份运行
- BCB Delphi WIN7 默认 以管理员身份运行 程序
- The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class fi
- Android Battery(四) 电池管理
- 学习中遇到的问题--json显示带小数点数字时被转义
- 面试题中的位运算
- Android 柱形WaveView
- C#默认以管理员身份运行程序
- POJ 2299(树状数组,离散化)
- 抽象基类
- 剑指offer系列之56:二叉树的下一个节点
- 冒泡排序、插入排序、交换排序
- 哈希表(等概率情况下)查找成功与查找不成功的平均查找长度
- 双指针---Remove Element
- 提升物理机利用率-Mysql多实例的配置
- AndroidStudio断点调试