手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#中用管理员身份运行程序代码实例
C#中用管理员身份运行程序代码实例
摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSyst...

using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace MyWebBrowser { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { //获得当前登录的Windows用户标示 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); //判断当前登录用户是否为管理员 if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) { //如果是管理员,则直接运行 Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } else { //创建启动对象 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); //设置运行文件 startInfo.FileName = System.Windows.Forms.Application.ExecutablePath; //设置启动动作,确保以管理员身份运行 startInfo.Verb = "runas"; //如果不是管理员,则启动UAC System.Diagnostics.Process.Start(startInfo); //退出 System.Windows.Forms.Application.Exit(); } } } }

【C#中用管理员身份运行程序代码实例】相关文章:

C#简单的加密类实例

C#中静态的深入理解

使用C#调用系统API实现内存注入的代码

c# 共享状态的文件读写实现代码

C#的XML两种代码注释实例说明

C#身份证验证小例子

c# 调用.bat文件的实现代码

webBrowser代理设置c#代码

C#中实现网段扫描的代码

C# 禁止应用程序多次启动的实例

精品推荐
分类导航