C# 讓程序自動以管理員身份運行


在程序中加入MANIFEST資源: 

1) 打開Vs2005或vs2008工程,看在Properties下是否有app.manifest這個文件;如沒有,右擊工程在菜單中選擇“屬性”,出現界面如下:

選中"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"后,重新編譯即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM