Winform 獲取Win7 UAC 管理員權限


1.新建app.manifest 寫入配置內容  

<?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>
    </security>
  </trustInfo>
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!-- 此應用程序設計使用的所有 Windows 版本的列表。Windows 將會自動選擇最兼容的環境。-->
      <!-- 如果應用程序設計使用 Windows 7,請取消注釋以下 supportedOS 節點-->
      <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>-->
    </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>


2.修改requestedExecutionLevel 中的level

3.在項目屬性中選擇應用程序項,然后在清單中選擇剛才加入的文件app.manifest。

4.重新編譯程序

5.在bin下運行程序可以看到圖標下已經出現標識


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM