1. 程式人生 > >讓程式自動以管理員身份執行

讓程式自動以管理員身份執行

C#:

1) 開啟Vs2005vs2008工程,看在Properties下是否有app.manifest這個檔案;如沒有,右擊工程在選單中選擇“屬性”,

  選中"Security",在介面中勾選"Enable ClickOnce Security Settings"後,在Properties下就有自動生成app.manifest檔案。

  開啟app.manifest檔案,將

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

  改為

<requestedExecutionLevel level="requireAdministrator"

uiAccess="false" />

  修改後的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 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><applicationRequestMinimum><defaultAssemblyRequest permissionSetReference="Custom"/><PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site"/></applicationRequestMinimum></security></trustInfo></asmv1:assembly>

  然後在"Security"中再勾去"Enable ClickOnce Security Settings"後,重新編譯即可。

Delphi:

1.建立 res 檔案建立一個文字檔案,名字可以自己起,如UAC.manifest,內容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>


建立文字檔案,名為 UAC.rc,內容:


1 24 UAC.manifest
編譯成 uac.res 檔案,執行:


brcc32 uac.rc -fouac.res
2.
在程式碼中引入
開啟專案檔案,加入

{$R uac.res}
3.
編譯程式
這時程式就支援 VISTA 了,在執行的時候,會彈出 WINDOWS 的提示框,詢問使用者是否允許以管理員身份執行。