在VS2010 IDE开发环境中,查看当前项目中是否包含了app.manifest文件,如果没有,则添加“应用程序清单文件”。
双击此文件,发现其是一xml格式的文件,有节点如下:
<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="highestAvailable" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
文件中相关节点的注释已经说得很明确,具体参数的解释如下,参考自链接 http://msdn.microsoft.com/zh-cn/library/bb384691.aspx:
-
asInvoker:应用程序将使用与启动它的进程相同的权限运行。 可通过选择“以管理员身份运行”将应用程序提升为更高权限。
-
highestAvailable:应用程序将使用可能的最高权限级别运行。 如果启动该应用程序的用户为管理员组的一个成员,则此选项与 requireAdministrator 相同。 如果可用的最高权限级别高于打开进程的级别,则系统将提示提供凭据。
-
requireAdministrator:应用程序将使用管理员权限运行。 启动该应用程序的用户必须是管理员组的一个成员。 如果打开进程未使用管理权限运行,则系统将提示提供凭据。
下面是判断当前程序是否运行于管理员身份下的一段代码(需要 using System.Security.Principal; ):
//用户
WindowsIdentity identity = WindowsIdentity.GetCurrent();
//用户组
WindowsPrincipal principal = new WindowsPrincipal(identity);
if (principal.IsInRole(WindowsBuiltInRole.Administrator) == true)
{
MessageBox.Show("当前程序处于管理员身份运行下!");
}
else
{
MessageBox.Show("当前程序不处于管理员身份运行下");
}