C# 让程序自动以管理员身份运行

本文介绍在Windows Vista或Win7系统中如何让应用程序以管理员权限运行的方法,包括通过修改EXE文件属性及在程序中加入MANIFEST资源两种途径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://tech.it168.com/a2011/0728/1224/000001224704_all.shtml


  【IT168 技术】exe在Vista或Win7下不以管理员权限运行,会被UAC(用户帐户控制)阻止访问系统某些功能,如修改注册表操作等;如何让exe以管理员权限运行呢,方法有两种,一个是直接修改exe属性;另一个是在程序中加入MANIFEST资源,下面分别介绍。

  1、直接修改exe属性

  1) 右击“exe”,在弹出的菜单中选择“属性”,出现的界面如下图:

  C# 让程序自动以管理员身份运行

  2) 选择"Compatibility"项,并勾选"Run this program as administrator"项即可。

  2、 在程序中加入MANIFEST资源

  1) 打开Vs2005或vs2008工程,看在Properties下是否有app.manifest这个文件;如没有,右击工程在菜单中选择“属性”,出现界面如下图:

  C# 让程序自动以管理员身份运行


  选中"Security",在界面中勾选"Enable ClickOnce Security Settings"后,在Properties下就有自动生成app.manifest文件。

  打开app.manifest文件,将

< requestedExecutionLevel level = " asInvoker "  uiAccess = " false "   />

  改为

< requestedExecutionLevel level = " requireAdministrator "  uiAccess = " false "   />

  修改后的app.manifest为:

<? xml version="1.0" encoding="utf-8" ?>
< asmv1:assembly  manifestVersion ="1.0"  xmlns ="urn:schemas-microsoft-com:asm.v1"  xmlns:asmv1 ="urn:schemas-microsoft-com:asm.v1"  xmlns:asmv2 ="urn:schemas-microsoft-com:asm.v2"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
< assemblyIdentity  version ="1.0.0.0"  name ="MyApplication.app"   />
< trustInfo  xmlns ="urn:schemas-microsoft-com:asm.v2" >
< security >
< requestedPrivileges  xmlns ="urn:schemas-microsoft-com:asm.v3" >
<!--  UAC Manifest Options
If you want to change the Windows User Account Control level replace the 
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
If you want to utilize File and Registry Virtualization for backward 
compatibility then delete the requestedExecutionLevel node.
-->
< requestedExecutionLevel  level ="requireAdministrator"  uiAccess ="false"   />
</ requestedPrivileges >
< applicationRequestMinimum >
< defaultAssemblyRequest  permissionSetReference ="Custom"   />
< PermissionSet  class ="System.Security.PermissionSet"  version ="1"  Unrestricted ="true"  ID ="Custom"  SameSite ="site"   />
</ applicationRequestMinimum >
</ security >
</ trustInfo >
</ asmv1:assembly >

  然后在"Security"中再勾去"Enable ClickOnce Security Settings"后,重新编译即可。

### C# 程序设置自动管理员权限启动 为了确保 C# 应用程序能够在启动自动获得管理员权限,可以按照以下方法进行配置: #### 修改项目属性和清单文件 在 Visual Studio 中开发的应用可以通过修改项目的属性来强制其始终以管理员权限运行。这涉及到编辑应用程序的 `app.manifest` 文件。 1. **打开项目属性** 右键单击解决方案资源管理器中的项目名称,选择“属性”,然后导航至“安全性”选项卡[^1]。 2. **启用 ClickOnce 安全设置 (可选)** 如果之前启用了 ClickOnce 安全性,则应先禁用它以免影响后续操作。取消勾选“启用 ClickOnce 安全设置”。 3. **添加或更新 app.manifest 文件** 当前项目可能已经包含了默认的 `app.manifest` 文件;如果没有的话,可以在项目根目录下手动添加此文件。接着,在该文件内找到如下 XML 节点,并将其 `<requestedExecutionLevel>` 的 level 属性更改为 `"requireAdministrator"`: ```xml <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> <security> <requestedPrivileges> <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/> </requestedPrivileges> </security> </trustInfo> ``` 通过上述更改,应用将在每次启动尝试获取提升后的权限级别[^3]。 #### 处理 UAC 提示窗口 值得注意的是,默认情况下 Windows 用户账户控制(UAC)会在请求更高权限显示确认对话框给用户。对于某些场景而言,这种交互可能是不必要的或者是不可接受的用户体验。要绕过这一机制,一种常见的做法是在特定条件下调整系统的注册表项,但这通常只适用于开发者自己的机器而不推荐用于生产环境部署。 ```csharp // 示例代码片段展示如何检测当前进程是否有足够的权限 using System; using System.Security.Principal; public static bool IsRunAsAdmin() { var identity = WindowsIdentity.GetCurrent(); var principal = new WindowsPrincipal(identity); return principal.IsInRole(WindowsBuiltInRole.Administrator); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值