Csharp/C#教程: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(); } } } }    您可能感兴趣的文章:C#运行CGI程序实例C#实现只运行单个实例应用程序的方法(使用VB.Net的IsSingleInstance)C#测量程序运行时间及cpu使用时间实例方法C#开发Windows服务实例之实现禁止QQ运行解决C#程序只允许运行一个实例的几种方法详解C#确保只有一个实例在运行的方法

标签: 程序 运行

C++事件驱动型银行排队模拟

学好C++必须做到的50条 绝对经典!

上述就是C#学习教程:C#中用管理员身份运行程序代码实例分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/906038.html

(0)
上一篇 2021年10月23日
下一篇 2021年10月23日

精彩推荐