1. 程式人生 > >[Winform]默認以管理員身份運行程序

[Winform]默認以管理員身份運行程序

項目 解決 sso logs Coding -i ces 利用 管理員

摘要

在使用setupfactory打包之後,想讓程序默認以管理員身份運行,因為涉及到創建文件刪除文件的操作,如果權限比較低的話,會出現沒有權限操作的bug。

解決辦法

在項目中找到app.manifest清單文件

技術分享

修改requestedExecutionLevel 的屬性level為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" Unrestricted="true" ID="Custom" SameSite="site" /> </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>--> <!-- 如果應用程序設計為使用 Windows 8.1,請取消對以下 supportedOS 節點的註釋--> <!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>--> </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>

這樣安裝之後,快捷方式上就會出現盾牌的圖標了,這樣在操作文件的時候就有權限了。

[Winform]默認以管理員身份運行程序