C# WinForm判斷程序是否以管理員身份運行_.Net教程
推薦:.net中使用DatagridView的增刪改方法default.aspx 頁面: %@ Page Language=C# AutoEventWireup=true CodeBehind=Default.aspx.cs Inherits=GPS_Web.Default % !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd html xmlns=http://
Vista 和 Windows 7 操作系統為了加強安全,增加了 UAC(用戶賬戶控制) 的機制,如果 UAC 被打開,用戶即使是以管理員權限登錄,其應用程序默認情況下也無法對系統目錄,系統注冊表等可能影響系統運行的設置進行寫操作。這個機制大大增強了系統的安全性,但對應用程序開發者來說,我們不能強迫用戶去關閉UAC,但有時我們開發的應用程序又需要以 Administrator 的方式運行,即 Win7 中 以 as administrator 方式運行,那么我們怎么來實現這樣的功能呢?
我們在 win7 下運行一些安裝程序時,會發現首先彈出一個對話框,讓用戶確認是否同意允許這個程序改變你的計算機配置,但我們編寫的應用程序默認是不會彈出這個提示的,也無法以管理員權限運行。本文介紹了 C# 程序如何設置來提示用戶以管理員權限運行。 首先在項目中增加一個 Application Manifest File
默認的配置如下: <?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個值可供選擇,如下表所示:
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>
分享:C#中調用Windows API時的數據類型對應關系BOOL=System.Int32 BOOLEAN=System.Int32 BYTE=System.UInt16 CHAR=System.Int16 COLORREF=System.UInt32 DWORD=System.UInt32 DWORD32=System.UInt32 DWORD64=System.UInt64 FLOAT=System.Float HACCEL=System.IntPtr HANDLE=System.IntPtr HBITMAP=System.IntPtr HBR
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發送Email實例(可帶附件)
- js實現廣告漂浮效果的小例子
- asp.net Repeater 數據綁定的具體實現
- Asp.Net 無刷新文件上傳并顯示進度條的實現方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報表的使用方法
- ASP.NET中操作SQL數據庫(連接字符串的配置及獲取)
- asp.net頁面傳值測試實例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲過程實現分頁示例代碼
- 相關鏈接:
- 教程說明:
.Net教程-C# WinForm判斷程序是否以管理員身份運行
。