若是程序不是以管理員身份運行,操做本地文件會提示:System.UnauthorizedAccessException異常。html
Vista 和 Windows 7 操做系統爲了增強安全,增長了 UAC(用戶帳戶控制) 的機制,若是 UAC 被打開,用戶即便是以管理員權限登陸,其應用程序默認狀況下也沒法對系統目錄,系統註冊表等可能影響系統運行的設置進行寫操做。這個機制大大加強了系統的安全性,但對應用程序開發者來講,咱們不能強迫用戶去關閉UAC,但有時咱們開發的應用程序又須要以 Administrator 的方式運行,即 Win7 中 以 as administrator 方式運行,那麼咱們怎麼來實現這樣的功能呢?
node
咱們在 win7 下運行一些安裝程序時,會發現首先彈出一個對話框,讓用戶確認是否贊成容許這個程序改變你的計算機配置,但咱們編寫的應用程序默認是不會彈出這個提示的,也沒法以管理員權限運行。本文介紹了 C# 程序如何設置來提示用戶以管理員權限運行。安全
首先在項目中增長一個 Application Manifest Fileapp
默認的配置以下:
ide
<?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="asInvoker" uiAccess="false" /> </requestedPrivileges> </security> </trustInfo> </asmv1:assembly>
Value | Description | Comment |
asInvoker | The application runs with the same access token as the parent process. | Recommended for standard user applications. Do refractoring with internal elevation points, as per the guidance provided earlier in this document. |
highestAvailable | The application runs with the highest privileges the current user can obtain. | Recommended for mixed-mode applications. Plan to refractor the application in a future release. |
requireAdministrator | The application runs only for administrators and requires that the application be launched with the full access token of an administrator. | Recommended for administrator only applications. Internal elevation points are not needed. The application is already running elevated. |
asInvoker : 若是選這個,應用程序就是以當前的權限運行。函數
highestAvailable: 這個是以當前用戶能夠得到的最高權限運行。ui
requireAdministrator: 這個是僅以系統管理員權限運行。this
默認狀況下是 asInvoker。
下面是修改後的配置文件:
spa
<?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 後,程序才能夠繼續運行,而且得到系統管理員的權限。
操作系統
下面再來看看程序如何知道當前運行在系統管理員權限仍是非系統管理員權限:
using System.Security.Principal; public static bool IsAdministrator() { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); return principal.IsInRole(WindowsBuiltInRole.Administrator); }這段代碼能夠用於判斷當前程序是否運行在系統管理員權限下。若是配置爲 asInvoker,在win7 下,這個函數會返回 false ,若是是 requireAdministrator 則返回 true。