標籤:
開啟VS2005、VS2008、VS2010工程,查看工程檔案夾中的Properties檔案夾下是否有app.manifest這個檔案;如沒有,按如下方式建立:滑鼠右擊工程在菜單中選擇“屬性”,點擊工程屬性的“安全性”標籤,在安全性標籤頁中勾選“啟用ClickOnce安全設定”,並選擇“這是完全可信的應用程式”,儲存工程,此時在Properties下已經自動產生了app.manifest檔案。
將預設的app.manifest檔案修改為
[html] view plain copy
- <?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>
設定檔修改後,我們運行應用程式,就會首先彈出這樣一個提示框,點 Yes 後,程式才可以繼續運行。
順便說下,還可以通過一個方法瞭解到此時程式運行是不是管理員權限:
[csharp] view plain copy
- public static bool IsAdministrator()
- {
- WindowsIdentity identity = WindowsIdentity.GetCurrent();
- WindowsPrincipal principal = new WindowsPrincipal(identity);
- return principal.IsInRole(WindowsBuiltInRole.Administrator);
- }
對於XML檔案中引用的UAC執行權限等級,分別代表下列含義:
asInvoker : 應用程式就是以當前的許可權運行。
highestAvailable: 這個是以目前使用者可以獲得的最高許可權運行。
requireAdministrator: 這個是僅以系統管理員許可權運行。
預設情況下是 asInvoker。
highestAvailable 和 requireAdministrator 這兩個選項都可以提示使用者擷取系統管理員許可權。那麼這兩個選項的區別在哪裡呢?
他們的區別在於,如果我們不是以管理員帳號登入,那麼如果應用程式設定為 requireAdministrator ,那麼應用程式就直接運行失敗,無法啟動。而如果設定為 highestAvailable,則應用程式可以運行成功,但是是以當前帳號的許可權運行而不是系統管理員許可權運行。如果我們希望程式在非管理員帳號登入時也可以運行(這種情況下應該某些功能受限制) ,那麼建議採用 highestAvailable 來配置。
vs2005 ,2008,2010中引入app.manifest(即c#程式在win7下以管理員權限運行方法)