<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      eaglet

      本博專注于基于微軟技術的搜索相關技術
        博客園  :: 首頁  :: 新隨筆  :: 聯系 :: 訂閱 訂閱  :: 管理

      如何讓 Win7 下的桌面應用程序以管理員權限運行

      Posted on 2011-04-22 08:34  eaglet  閱讀(26581)  評論(18)    收藏  舉報

      Vista 和 Windows 7 操作系統為了加強安全,增加了 UAC(用戶賬戶控制) 的機制,如果 UAC 被打開,用戶即使是以管理員權限登錄,其應用程序默認情況下也無法對系統目錄,系統注冊表等可能影響系統運行的設置進行寫操作。這個機制大大增強了系統的安全性,但對應用程序開發者來說,我們不能強迫用戶去關閉UAC,但有時我們開發的應用程序又需要以 Administrator 的方式運行,即 Win7 中 以 as administrator 方式運行,那么我們怎么來實現這樣的功能呢?

       

      我們在 win7 下運行一些安裝程序時,會發現首先彈出一個對話框,讓用戶確認是否同意允許這個程序改變你的計算機配置,但我們編寫的應用程序默認是不會彈出這個提示的,也無法以管理員權限運行。本文介紹了 C# 程序如何設置來提示用戶以管理員權限運行。

      首先在項目中增加一個 Application Manifest File

       

      image

       

      默認的配置如下:

      <?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>
      

       

      我們可以看到這個配置中有一個 requestedExecutionLevel 項,這個項用于配置當前應用請求的執行權限級別。這個項有3個值可供選擇,如下表所示:

       

      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: 這個是以當前用戶可以獲得的最高權限運行。

      requireAdministrator: 這個是僅以系統管理員權限運行。

       

      默認情況下是 asInvoker。

      highestAvailable 和 requireAdministrator 這兩個選項都可以提示用戶獲取系統管理員權限。那么這兩個選項的區別在哪里呢?

      他們的區別在于,如果我們不是以管理員帳號登錄,那么如果應用程序設置為 requireAdministrator ,那么應用程序就直接運行失敗,無法啟動。而如果設置為 highestAvailable,則應用程序可以運行成功,但是是以當前帳號的權限運行而不是系統管理員權限運行。如果我們希望程序在非管理員帳號登錄時也可以運行(這種情況下應該某些功能受限制) ,那么建議采用 highestAvailable 來配置。

      關于requestedExecutionLevel 設置的權威文檔請參考下面鏈接:

      Create and Embed an Application Manifest (UAC)

       

      下面是修改后的配置文件:

      <?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 后,程序才可以繼續運行,并且獲得系統管理員的權限。

      image

       

      下面再來看看程序如何知道當前運行在系統管理員權限還是非系統管理員權限:

       

              public static bool IsAdministrator()
              {
                  WindowsIdentity identity = WindowsIdentity.GetCurrent();
                  WindowsPrincipal principal = new WindowsPrincipal(identity);
                  return principal.IsInRole(WindowsBuiltInRole.Administrator);
              }

       

      這段代碼可以用于判斷當前程序是否運行在系統管理員權限下。如果配置為 asInvoker,在win7 下,這個函數會返回 false ,如果是 requireAdministrator  則返回 true。

      主站蜘蛛池模板: 婷婷久久综合九色综合88| 中国性欧美videofree精品| 国产高清自产拍av在线| 久久久久国精品产熟女久色| 日韩理伦片一区二区三区| 国产精品无码无卡在线观看久| 中文字幕人成无码免费视频| 久久精品人成免费| 欧美人禽zozo动人物杂交| 久久精品国产99久久无毒不卡| 欧美成人精品三级网站| 中文字幕无线码中文字幕| 少妇无套内射中出视频| 国产成人啪精品视频免费APP| 久久国产精品波多野结衣| 国产乱沈阳女人高潮乱叫老 | 亚洲精品综合一区二区三区在线| 鲁一鲁一鲁一鲁一澡| 国产一区二区三区美女| 美日韩av一区二区三区| 国产精品美女久久久久久麻豆 | 午夜高清福利在线观看| 无码人妻丝袜在线视频红杏| 大乳丰满人妻中文字幕日本| 亚洲精品无码高潮喷水A| 欧美日本一区二区视频在线观看| 禹州市| 26uuu另类亚洲欧美日本| 中文字幕精品亚洲二区| 亚洲色大成网站WWW国产| 国产精品一区二区传媒蜜臀| 亚洲欧美色综合影院| 国产一区二区三区四区色| 精品人妻二区中文字幕| 日韩精品国产中文字幕| 国产短视频一区二区三区| 中文字幕午夜福利片午夜福利片97 | 国产精品亚洲а∨无码播放| 精品亚洲国产成人av在线| 亚洲a成人片在线观看| 你拍自拍亚洲一区二区三区|