C#中用管理员身份运行程序代码实例
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Windows.Forms;
namespaceMyWebBrowser
{
staticclassProgram
{
///<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
staticvoidMain()
{
//获得当前登录的Windows用户标示
System.Security.Principal.WindowsIdentityidentity=System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipalprincipal=newSystem.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if(principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
//如果是管理员,则直接运行
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(newForm1());
}
else
{
//创建启动对象
System.Diagnostics.ProcessStartInfostartInfo=newSystem.Diagnostics.ProcessStartInfo();
//设置运行文件
startInfo.FileName=System.Windows.Forms.Application.ExecutablePath;
//设置启动动作,确保以管理员身份运行
startInfo.Verb="runas";
//如果不是管理员,则启动UAC
System.Diagnostics.Process.Start(startInfo);
//退出
System.Windows.Forms.Application.Exit();
}
}
}
}
热门推荐
10 病人祝福语老师寄语简短
11 新娘生孩子祝福语简短
12 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短