C# WinForm判斷程序是否以管理員身份運(yùn)行_.Net教程
推薦:.net中使用DatagridView的增刪改方法default.aspx 頁(yè)面: %@ 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 操作系統(tǒng)為了加強(qiáng)安全,增加了 UAC(用戶賬戶控制) 的機(jī)制,如果 UAC 被打開(kāi),用戶即使是以管理員權(quán)限登錄,其應(yīng)用程序默認(rèn)情況下也無(wú)法對(duì)系統(tǒng)目錄,系統(tǒng)注冊(cè)表等可能影響系統(tǒng)運(yùn)行的設(shè)置進(jìn)行寫(xiě)操作。這個(gè)機(jī)制大大增強(qiáng)了系統(tǒng)的安全性,但對(duì)應(yīng)用程序開(kāi)發(fā)者來(lái)說(shuō),我們不能強(qiáng)迫用戶去關(guān)閉UAC,但有時(shí)我們開(kāi)發(fā)的應(yīng)用程序又需要以 Administrator 的方式運(yùn)行,即 Win7 中 以 as administrator 方式運(yùn)行,那么我們?cè)趺磥?lái)實(shí)現(xiàn)這樣的功能呢?
我們?cè)?win7 下運(yùn)行一些安裝程序時(shí),會(huì)發(fā)現(xiàn)首先彈出一個(gè)對(duì)話框,讓用戶確認(rèn)是否同意允許這個(gè)程序改變你的計(jì)算機(jī)配置,但我們編寫(xiě)的應(yīng)用程序默認(rèn)是不會(huì)彈出這個(gè)提示的,也無(wú)法以管理員權(quán)限運(yùn)行。本文介紹了 C# 程序如何設(shè)置來(lái)提示用戶以管理員權(quán)限運(yùn)行。 首先在項(xiàng)目中增加一個(gè) Application Manifest File
默認(rèn)的配置如下: <?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>
我們可以看到這個(gè)配置中有一個(gè) requestedExecutionLevel 項(xiàng),這個(gè)項(xiàng)用于配置當(dāng)前應(yīng)用請(qǐng)求的執(zhí)行權(quán)限級(jí)別。這個(gè)項(xiàng)有3個(gè)值可供選擇,如下表所示:
asInvoker : 如果選這個(gè),應(yīng)用程序就是以當(dāng)前的權(quán)限運(yùn)行。
highestAvailable: 這個(gè)是以當(dāng)前用戶可以獲得的最高權(quán)限運(yùn)行。
requireAdministrator: 這個(gè)是僅以系統(tǒng)管理員權(quán)限運(yùn)行。
默認(rèn)情況下是 asInvoker。
highestAvailable 和 requireAdministrator 這兩個(gè)選項(xiàng)都可以提示用戶獲取系統(tǒng)管理員權(quán)限。那么這兩個(gè)選項(xiàng)的區(qū)別在哪里呢?
他們的區(qū)別在于,如果我們不是以管理員帳號(hào)登錄,那么如果應(yīng)用程序設(shè)置為 requireAdministrator ,那么應(yīng)用程序就直接運(yùn)行失敗,無(wú)法啟動(dòng)。而如果設(shè)置為 highestAvailable,則應(yīng)用程序可以運(yùn)行成功,但是是以當(dāng)前帳號(hào)的權(quán)限運(yùn)行而不是系統(tǒng)管理員權(quán)限運(yùn)行。如果我們希望程序在非管理員帳號(hào)登錄時(shí)也可以運(yùn)行(這種情況下應(yīng)該某些功能受限制) ,那么建議采用 highestAvailable 來(lái)配置。
關(guān)于requestedExecutionLevel 設(shè)置的權(quán)威文檔請(qǐng)參考下面鏈接:
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#中調(diào)用Windows API時(shí)的數(shù)據(jù)類型對(duì)應(yīng)關(guān)系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發(fā)送Email實(shí)例(可帶附件)
- js實(shí)現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)
- Asp.Net 無(wú)刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- Asp.net獲取客戶端IP常見(jiàn)代碼存在的偽造IP問(wèn)題探討
- VS2010 水晶報(bào)表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(kù)(連接字符串的配置及獲取)
- asp.net頁(yè)面?zhèn)髦禍y(cè)試實(shí)例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲(chǔ)過(guò)程實(shí)現(xiàn)分頁(yè)示例代碼
.Net教程Rss訂閱編程教程搜索
.Net教程推薦
- 解析利用wsdl.exe生成webservice代理類的詳解
- 基于C#的接口基礎(chǔ)教程之二
- 模板無(wú)憂:asp.net后臺(tái)cs中的JSON格式變量在前臺(tái)Js中調(diào)用方法
- asp.net 2.0中用GRIDVIEW插入新記錄
- 用Java發(fā)送圖文并茂的HTML郵件
- 基于.NET平臺(tái)的分層架構(gòu)實(shí)戰(zhàn)(四)實(shí)體類的設(shè)計(jì)與實(shí)現(xiàn)
- 解決ASP.NET 2.0中CSS失效的問(wèn)題
- 近期的幾個(gè)ASP.NET開(kāi)發(fā)經(jīng)驗(yàn)總結(jié)和收集
- 對(duì)數(shù)據(jù)訪問(wèn)層第一種實(shí)現(xiàn)(Acc SQL)的重構(gòu)
- ASP.Net網(wǎng)絡(luò)數(shù)據(jù)庫(kù):連接到數(shù)據(jù)庫(kù)
- 相關(guān)鏈接:
- 教程說(shuō)明:
.Net教程-C# WinForm判斷程序是否以管理員身份運(yùn)行
。