以管理员方式运行程序

1、C++程序:

编译程序的时候设置一下 在项目属性–连接器–清单文件–UAC执行级别改为requireAdministrator

2、C#程序:

C# 程序如何设置来提示用户以管理员权限运行。

首先在项目中增加一个 “应用程序清单文件”

默认的配置如下:

<?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 清单选项

            如果要更改 Windows 用户帐户控制级别,请用以下节点之一替换   requestedExecutionLevel 节点。

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

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

       <requestedExecutionLevel  level=”highestAvailable” uiAccess=”false” />

            指定 requestedExecutionLevel 节点将会禁用文件和注册表虚拟化。

            如果要利用文件和注册表虚拟化实现向后

            兼容性,则删除 requestedExecutionLevel 节点。         –>

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

    </requestedPrivileges>    

</security>

  </trustInfo>

    <compatibility xmlns=”urn:schemas-microsoft-com:compatibility.v1″>

    <application>

      <!– 此应用程序设计使用的所有 Windows 版本的列表。Windows 将会自动选择最兼容的环境。–>

      <!– 如果应用程序设计使用 Windows 7,请取消注释以下 supportedOS 节点–>

      <!–<supportedOS Id=”{35138b9a-5d96-4fbd-8e2d-a2440225f93a}”/>–>

          </application>

  </compatibility>

    <!– 启用 Windows 公共控件和对话框的主题(Windows XP 和更高版本) –>

  <!– <dependency>  

   <dependentAssembly>

      <assemblyIdentity           type=”win32″           name=”Microsoft.Windows.Common-Controls”           version=”6.0.0.0″           processorArchitecture=”*”           publicKeyToken=”6595b64144ccf1df”           language=”*”         />     </dependentAssembly>   </dependency>–>

</asmv1:assembly>


我们可以看到这个配置中有一个 requestedExecutionLevel 项,这个项用于配置当前应用请求的执行权限级别。这个项有3个值可供选择,如下表所示:

Value Description Comment
asInvoker The application runs with the same access token as the parent process. Recommended for standard user applications. Do refractoring with internal elevation points, as per the guidance provided earlier in this document.
highestAvailable The application runs with the highest privileges the current user can obtain. Recommended for mixed-mode applications. Plan to refractor the application in a future release.
requireAdministrator The application runs only for administrators and requires that the application be launched with the full access token of an administrator. Recommended for administrator only applications. Internal elevation points are not needed. The application is already running elevated.

asInvoker : 如果选这个,应用程序就是以当前的权限运行。

highestAvailable: 这个是以当前用户可以获得的最高权限运行。

requireAdministrator: 这个是仅以系统管理员权限运行。

默认情况下是 asInvoker。

highestAvailable 和 requireAdministrator 这两个选项都可以提示用户获取系统管理员权限。那么这两个选项的区别在哪里呢?

他们的区别在于,如果我们不是以管理员帐号登录,那么如果应用程序设置为 requireAdministrator ,那么应用程序就直接运行失败,无法启动。而如果设置为 highestAvailable,则应用程序可以运行成功,但是是以当前帐号的权限运行而不是系统管理员权限运行。如果我们希望程序在非管理员帐号登录时也可以运行(这种情况下应该某些功能受限制) ,那么建议采用 highestAvailable 来配置。

配置文件修改后,我们运行应用程序,就会首先弹出这样一个提示框,点 Yes 后,程序才可以继续运行

版权声明:本文为shuiguang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/shuiguang/archive/2013/04/10/3011627.html