vs2005 ,2008,2010中引入app.manifest(即c#程式在win7下以管理員權限運行方法)

來源:互聯網
上載者:User

標籤:

 開啟VS2005、VS2008、VS2010工程,查看工程檔案夾中的Properties檔案夾下是否有app.manifest這個檔案;如沒有,按如下方式建立:滑鼠右擊工程在菜單中選擇“屬性”,點擊工程屬性的“安全性”標籤,在安全性標籤頁中勾選“啟用ClickOnce安全設定”,並選擇“這是完全可信的應用程式”,儲存工程,此時在Properties下已經自動產生了app.manifest檔案。

        將預設的app.manifest檔案修改為

[html] view plain copy 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"   
  3.   
  4. xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"  
  5.   
  6.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  7.   <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>  
  8.   <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">  
  9.     <security>  
  10.       <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">  
  11.         <!-- UAC Manifest Options  
  12.             If you want to change the Windows User Account Control level replace the   
  13.             requestedExecutionLevel node with one of the following.  
  14.         <requestedExecutionLevel  level="asInvoker" uiAccess="false" />  
  15.         <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />  
  16.         <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />  
  17.             If you want to utilize File and Registry Virtualization for backward   
  18.             compatibility then delete the requestedExecutionLevel node.  
  19.         -->  
  20.         <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />  
  21.       </requestedPrivileges>  
  22.     </security>  
  23.   </trustInfo>  
  24. </asmv1:assembly>  

 

        設定檔修改後,我們運行應用程式,就會首先彈出這樣一個提示框,點 Yes 後,程式才可以繼續運行。

        順便說下,還可以通過一個方法瞭解到此時程式運行是不是管理員權限:

 

[csharp] view plain copy 
  1. public static bool IsAdministrator()  
  2. {  
  3.     WindowsIdentity identity = WindowsIdentity.GetCurrent();  
  4.     WindowsPrincipal principal = new WindowsPrincipal(identity);  
  5.     return principal.IsInRole(WindowsBuiltInRole.Administrator);  
  6. }  


對於XML檔案中引用的UAC執行權限等級,分別代表下列含義:

 

 

         asInvoker : 應用程式就是以當前的許可權運行。

         highestAvailable: 這個是以目前使用者可以獲得的最高許可權運行。

         requireAdministrator: 這個是僅以系統管理員許可權運行。

         預設情況下是 asInvoker。

         highestAvailable 和 requireAdministrator 這兩個選項都可以提示使用者擷取系統管理員許可權。那麼這兩個選項的區別在哪裡呢?

         他們的區別在於,如果我們不是以管理員帳號登入,那麼如果應用程式設定為 requireAdministrator ,那麼應用程式就直接運行失敗,無法啟動。而如果設定為 highestAvailable,則應用程式可以運行成功,但是是以當前帳號的許可權運行而不是系統管理員許可權運行。如果我們希望程式在非管理員帳號登入時也可以運行(這種情況下應該某些功能受限制) ,那麼建議採用 highestAvailable 來配置。

 

vs2005 ,2008,2010中引入app.manifest(即c#程式在win7下以管理員權限運行方法)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.