110,561
社区成员
发帖
与我相关
我的任务
分享
public static void GetAdmin(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
{
try
{
string sysFolder = Environment.GetFolderPath(Environment.SpecialFolder.System);
//MessageBox.Show(sysFolder);
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = false;
//设置运行文件
startInfo.FileName = Application.StartupPath + @"/Registry_Pro.EXE";
//System.Windows.Forms.Application.ExecutablePath;
//startInfo.UserName = Environment.UserName;
//startInfo.Domain = Environment.UserDomainName;
//设置启动参数
startInfo.Arguments = String.Join(" ", args);
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "runas";
//如果不是管理员,则启动UAC
Process p = System.Diagnostics.Process.Start(startInfo);
//p.WaitForInputIdle();
//p.WaitForExit();
}
catch (Exception e)
{
throw e;
}
//退出
//System.Windows.Forms.Application.Exit();
}
}