2009-11-23 101 views
5

由于Vista问世,我的一些.NET应用程序已经开始抛出安全性异常。以管理员身份运行.NET应用程序

我注意到一些应用程序(即我的防病毒软件,控制面板)有一个小盾牌,当我运行这些应用程序时,管理员权限由我自动从Windows请求。

我知道作为一个用户,我可以将应用程序设置为以管理员身份运行,但这还不够好,因为如果应用程序没有权限运行,它会在我的用户计算机上崩溃。

有没有办法告诉Windows(以编程方式)我希望应用程序以管理权限运行?

回答

16

创建一个应用程序清单,设定requestedExecutionLevel到requireAdminstrator:

举例(当您添加应用程序清单由VS生成):

<?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> 
    </security> 
    </trustInfo> 
</asmv1:assembly> 

如果您添加到一个Visual Studio应用程序项目,它将在编译时嵌入到您的程序集中。

相关问题