天天看點

[程式設計技巧]C#如何以管理者身份運作程式

     在使用winform程式擷取調用cmd指令提示符時,如果是win7以上的作業系統,會需要必須以管理者身份運作才會執行成功,否則無效果或提示錯誤。

     比如在通過winform程式執行cmd指令時,某些情況下如果不是以管理者身份運作,則會提示指令無效。

     或者通過winform程式執行Windows Service 服務時,也需要以管理者身份才能調用Service服務。

下面講解一下如何使程式擷取管理者權限來運作。

一: 在Visual Studio 中--解決方案資料總管--右鍵項目名稱--屬性,找到“安全性”選項,

[程式設計技巧]C#如何以管理者身份運作程式

二:勾選“啟用ClickOnce安全設定”,

[程式設計技巧]C#如何以管理者身份運作程式

三:這時,在項目下面會多出一個“app.manifest”的檔案,選中它,并找到代碼段<requestedExecutionLevel level="asInvoker" uiAccess="false" />,将其改為:<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />,

[程式設計技巧]C#如何以管理者身份運作程式

打開:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <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 節點。n
        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

            指定 requestedExecutionLevel 元素将禁用檔案和系統資料庫虛拟化。
            如果你的應用程式需要此虛拟化來實作向後相容性,則删除此
            元素。
        -->
        <requestedExecutionLevel level="asInvoker" uiAccess="false" />
      </requestedPrivileges>
      <applicationRequestMinimum>
        <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
        <defaultAssemblyRequest permissionSetReference="Custom" />
      </applicationRequestMinimum>
    </security>
  </trustInfo>
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!-- 設計此應用程式與其一起工作且已針對此應用程式進行測試的
           Windows 版本的清單。取消評論适當的元素,Windows 将
           自動選擇最相容的環境。 -->
      <!-- Windows Vista -->
      <!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
      <!-- Windows 7 -->
      <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
      <!-- Windows 8 -->
      <!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
      <!-- Windows 8.1 -->
      <!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
      <!-- Windows 10 -->
      <!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
    </application>
  </compatibility>
  <!-- 訓示該應用程式可以感覺 DPI 且 Windows 在 DPI 較高時将不會對其進行
       自動縮放。Windows Presentation Foundation (WPF)應用程式自動感覺 DPI,無需
       選擇加入。選擇加入此設定的 Windows 窗體應用程式(目标設定為 .NET Framework 4.6 )還應
       在其 app.config 中将 "EnableWindowsFormsHighDpiAutoResizing" 設定設定為 "true"。-->
  <!--
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
  </application>
  -->
  <!-- 啟用 Windows 公共控件和對話框的主題(Windows XP 和更高版本) -->
  <!--
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
          type="win32"
          name="Microsoft.Windows.Common-Controls"
          version="6.0.0.0"
          processorArchitecture="*"
          publicKeyToken="6595b64144ccf1df"
          language="*"
        />
    </dependentAssembly>
  </dependency>
  -->
</assembly>
           

修改為:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <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 節點。n
        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

            指定 requestedExecutionLevel 元素将禁用檔案和系統資料庫虛拟化。
            如果你的應用程式需要此虛拟化來實作向後相容性,則删除此
            元素。
        -->
        <!--<requestedExecutionLevel level="asInvoker" uiAccess="false" />-->
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
      <applicationRequestMinimum>
        <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
        <defaultAssemblyRequest permissionSetReference="Custom" />
      </applicationRequestMinimum>
    </security>
  </trustInfo>
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!-- 設計此應用程式與其一起工作且已針對此應用程式進行測試的
           Windows 版本的清單。取消評論适當的元素,Windows 将
           自動選擇最相容的環境。 -->
      <!-- Windows Vista -->
      <!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
      <!-- Windows 7 -->
      <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
      <!-- Windows 8 -->
      <!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
      <!-- Windows 8.1 -->
      <!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
      <!-- Windows 10 -->
      <!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
    </application>
  </compatibility>
  <!-- 訓示該應用程式可以感覺 DPI 且 Windows 在 DPI 較高時将不會對其進行
       自動縮放。Windows Presentation Foundation (WPF)應用程式自動感覺 DPI,無需
       選擇加入。選擇加入此設定的 Windows 窗體應用程式(目标設定為 .NET Framework 4.6 )還應
       在其 app.config 中将 "EnableWindowsFormsHighDpiAutoResizing" 設定設定為 "true"。-->
  <!--
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
  </application>
  -->
  <!-- 啟用 Windows 公共控件和對話框的主題(Windows XP 和更高版本) -->
  <!--
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
          type="win32"
          name="Microsoft.Windows.Common-Controls"
          version="6.0.0.0"
          processorArchitecture="*"
          publicKeyToken="6595b64144ccf1df"
          language="*"
        />
    </dependentAssembly>
  </dependency>
  -->
</assembly>
           

四:改正後,不要急于重新編譯生成,再次打開“屬性--安全性”界面,

将“啟用ClickOnce安全設定”前面的勾去掉後再編譯運作。 不然程式會報錯無法運作。

[程式設計技巧]C#如何以管理者身份運作程式

五:最後,儲存修改,重新編譯運作程式。

這時在部分系統中可以看到一個類似安全盾牌的圖示:

[程式設計技巧]C#如何以管理者身份運作程式

而打開程式時,會提示“使用者賬戶控制”來擷取管理者權限運作,點選“是”則擷取了管理者權限。

本文轉自:部落格園-酷小孩:C#如何以管理者身份運作程式

繼續閱讀