C#程序以管理员权限运行
在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也无法对系统目录、系统注册表等可能影响系统正常运行的设置进行写操作。这个机制大大增强了系统的安全性,但对应用程序开发者来说,我们不能强迫用户去关闭UAC,但有时我们开发的应用程序又需要以 Administrator 的方式运行,如何实现这样的功能呢?
下面演示 C# 程序如何实现提示用户以管理员权限运行。
本例以WinForm程序演示,新建一项目生成后进行相应修改:
方法一:通过 System.Diagnostics.Process.Start() 方式启动:
实现方法: 修改默认生成的Program文件,修改后的代码如下:
由于已经在代码上做了注释,所以不再详细说明;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection.Emit; using System.Security.Principal; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsUACRunAsAdministrator { internal static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault( false ); //在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也无法对系统目录、系统注册表等可能影响系统正常运行的设置进行写操作。这个机制大大增强了系统的安全性,但对应用程序开发者来说,我们不能强迫用户去关闭UAC,但有时我们开发的应用程序又需要以 Administrator 的方式运行,如何实现这样的功能呢? //下面演示 C# 程序如何实现提示用户以管理员权限运行。 //本例以WinForm程序演示,新建一项目生成后进行相应修改: //方法一:通过 System.Diagnostics.Process.Start() 方式启动: // 获取当前的windows 用户 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); //检查 获取当前的windows 用户 的 Windows 组成员身份。 System.Security.Principal.WindowsPrincipal windowsPrincipal = new System.Security.Principal.WindowsPrincipal(identity); //确定当前主体是否属于具有指定 System.Security.Principal.WindowsBuiltInRole 的 Windows 用户组。 //判断当前用户是否是管理员 if (windowsPrincipal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) { MessageBox.Show($ "{identity.Name}是管理员角色,管理员权限运行" ); //是管理员角色,管理员权限运行 Application.Run( new Form1()); } else { //创建启动对象 ProcessStartInfo processStartInfo= new ProcessStartInfo(); processStartInfo.UseShellExecute = false ; //processStartInfo.RedirectStandardOutput = true; //processStartInfo.RedirectStandardError = true; //processStartInfo.RedirectStandardInput = true; processStartInfo.CreateNoWindow = true ; processStartInfo.WorkingDirectory = Environment.CurrentDirectory; processStartInfo.FileName = Application.ExecutablePath; //设置启动动作确保 使用管理员权限运行 processStartInfo.Verb = "runas" ; try { Process.Start(processStartInfo); } catch (Exception) { return ; } //退出 Application.Exit(); } //方法二:通过添加应用程序清单文件: //在 项目 上 添加新项 选择“应用程序清单文件” 然后单击 添加 按钮 //添加后,默认打开app.manifest文件,将: //< requestedExecutionLevel level = "asInvoker" uiAccess = "false" /> //修改为: //< requestedExecutionLevel level = "requireAdministrator" uiAccess = "false" /> //然后打开 项目属性 ,将 应用程序 标签页中的 资源 中的 清单 修改为新建的 app.manifest。 //项目属性 => 应用程序 => 资源 => 清单 => app.manifest。 //重新生成项目,再次打开程序时就会提示 需要以管理员权限运行。 //需要注意的是:如果在VS中 启动调试 的话,就会提示 此任务要求应用程序具有提升的权限。如下图: } /// <summary> /// 确定当前主体是否属于具有指定 Administrator 的 Windows 用户组 /// </summary> /// <returns>如果当前主体是指定的 Administrator 用户组的成员,则为 true;否则为 false。</returns> public static bool IsAdministrator() { bool result; try { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); result = principal.IsInRole(WindowsBuiltInRole.Administrator); //http://www.cnblogs.com/Interkey/p/RunAsAdmin.html //AppDomain domain = Thread.GetDomain(); //domain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); //WindowsPrincipal windowsPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal; //result = windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator); } catch { result = false ; } return result; } } } |
效果:由于是通过System.Diagnostics.Process.Start() 方式外部调用启动,所以直接通过VS运行时,是不会提示VS也需要管理员权限,只有程序本身需要管理员权限,与生成应用程序的程序不同。这点是和方法二实现的主要不同之处。
本文地址:http://www.cnblogs.com/Interkey/p/RunAsAdmin.html
方法二:通过添加应用程序清单文件:
在 项目 上 添加新项 选择“应用程序清单文件” 然后单击 添加 按钮
添加后,默认打开app.manifest文件,将:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
修改为:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
然后打开 项目属性 ,将 应用程序 标签页中的 资源 中的 清单 修改为新建的 app.manifest。
重新生成项目,再次打开程序时就会提示 需要以管理员权限运行。
需要注意的是:如果在VS中 启动调试 的话,就会提示 此任务要求应用程序具有提升的权限。如下图:
判断程序是否以管理员身份运行
需要添加命名空间:
using System.Security.Principal;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /// <summary> /// 确定当前主体是否属于具有指定 Administrator 的 Windows 用户组 /// </summary> /// <returns>如果当前主体是指定的 Administrator 用户组的成员,则为 true;否则为 false。</returns> public static bool IsAdministrator() { bool result; try { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); result = principal.IsInRole(WindowsBuiltInRole.Administrator); //http://www.cnblogs.com/Interkey/p/RunAsAdmin.html //AppDomain domain = Thread.GetDomain(); //domain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); //WindowsPrincipal windowsPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal; //result = windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator); } catch { result = false ; } return result; } |
方法三:直接修改程序文件的属性
右击程序文件,在弹出的属性对话框中的 兼容性 标签页中
勾选“以管理员身份运行此程序”即可。
如果有兴趣还可以继续查看下面的链接:
http://www.cnblogs.com/Lemon_s/archive/2011/07/28/2119222.html
http://www.cnblogs.com/shenchao/archive/2013/03/05/2944660.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
2018-07-04 http站点转https站点教程
2018-07-04 在阿里云申请Symantec免费SSL证书操作流程
2018-07-04 http改造成https的实践案例
2017-07-04 Linq 联合条件查询快捷方法