標籤:
環境說明: VS2012,windows 7 親自驗證過win7 和xp ,XP直接不彈框,因為XP沒有UAC控制機制
步驟1:
右鍵項目--》屬性--》安全性--》選中【啟用ClickOnce安全設定】
此時在我們的項目下Properties目錄下多了個叫 app.manifest 的檔案
步驟2:
檔案裡面的代碼如下,我們只需要將以下這句更改了即可
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
改為
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
asInvoker : 如果選這個,應用程式就是以當前的許可權運行。
highestAvailable: 這個是以目前使用者可以獲得的最高許可權運行。
requireAdministrator: 這個是僅以系統管理員許可權運行。
預設情況下是 asInvoker。
highestAvailable 和 requireAdministrator 這兩個選項都可以提示使用者擷取系統管理員許可權。那麼這兩個選項的區別在哪裡呢?
區別即是,highestAvailable按當前帳號能擷取到的許可權執行,而requireAdministrator則是以具有完整許可權的管理員運 行。如果當前賬戶是管理員賬戶的話,那麼兩者都是可以的通過提升許可權來擷取到管理員權限的;而如果當前賬戶是Guest的話,那麼 highestAvailable則放棄提升許可權而直接運行,而requireAdministrator則允許輸入其他管理員賬戶的密碼來提升許可權。
以下是修改後的內容:
<?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 清單選項 如果要更改 Windows 使用者帳戶控制層級,請用以下節點之一替換 requestedExecutionLevel 節點。 <requestedExecutionLevel level="asInvoker" uiAccess="false" /> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> <requestedExecutionLevel level="highestAvailable" uiAccess="false" /> 指定 requestedExecutionLevel 節點將會禁用檔案及登錄模擬。 如果要利用檔案及登錄模擬實現向後 相容性,則刪除 requestedExecutionLevel 節點。 --> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> </requestedPrivileges> <applicationRequestMinimum> <defaultAssemblyRequest permissionSetReference="Custom" /> <PermissionSet class="System.Security.PermissionSet" version="1" ID="Custom" SameSite="none" /> </applicationRequestMinimum> </security> </trustInfo> <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> <application> <!-- 此應用程式設計使用的所有 Windows 版本的列表。 Windows 將會自動選擇最相容的環境。--> <!-- 如果應用程式設計為使用 Windows Vista,請取消注釋以下 supportedOS 節點--> <!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"></supportedOS>--> <!-- 如果應用程式設計使用 Windows 7,請取消注釋以下 supportedOS 節點--> <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>--> <!-- 如果應用程式設計為使用 Windows 8,請取消注釋以下 supportedOS 節點--> <!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"></supportedOS>--> </application> </compatibility> <!-- 啟用 Windows 公用控制項和對話方塊的主題(Windows XP 和更高版本) --> <!-- <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" /> </dependentAssembly> </dependency>--></asmv1:assembly>
步驟3:右鍵項目--》屬性--》安全性--》去掉【啟用ClickOnce安全設定】的勾,否則編譯會報錯
編譯後,我們換個非管理員權限的帳號登陸系統,開啟程式,可以看到需要提供管理員權限的彈框。
下面再來看看程式如何知道當前運行在系統管理員許可權還是非系統管理員許可權:
using System.Security.Principal
//驗證過win7 和xp 能行
public static bool IsAdministrator()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
有關更進一步的見解參加:
.NET中提升UAC許可權的方法總結 - 大魔王mAysWINd
c# 如何擷取系統管理員許可權(UAC) 及判斷當前是否是管理員權限