C# 讓程式自動以管理員身份執行
轉載: http://tech.it168.com/a2011/0728/1224/000001224704_all.shtml
【IT168 技術】exe在Vista或Win7下不以管理員許可權執行,會被UAC(使用者帳戶控制)阻止訪問系統某些功能,如修改登錄檔操作等;如何讓exe以管理員許可權執行呢,方法有兩種,一個是直接修改exe屬性;另一個是在程式中加入MANIFEST資源,下面分別介紹。
1、直接修改exe屬性
1) 右擊“exe”,在彈出的選單中選擇“屬性”,出現的介面如下圖:
2) 選擇"Compatibility"項,並勾選"Run this program as administrator"項即可。
2、 在程式中加入MANIFEST資源
1) 開啟Vs2005或vs2008工程,看在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.
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>
<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.
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"後,重新編譯即可。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22275400/viewspace-752763/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- C# 以管理員身份執行WinForm程式C#ORM
- 判斷當前程式是否"以管理員身份執行"的
- cmd怎麼以管理員身份執行 cmd切換管理員身份方法介紹
- win10系統怎麼以管理員身份執行檔案 win10以管理員身份執行的步驟Win10
- Win10系統下怎麼讓所有程式都預設以管理員身份執行Win10
- win10怎麼以管理員身份執行cmdWin10
- 如何讓QT程式以管理員許可權執行(UAC)QT
- 筆記本以管理員的身份執行在哪_筆記本怎麼以管理員身份執行電腦筆記
- 家庭版win10怎麼取消以管理員身份執行_win10家庭版取消以管理員身份執行的方法Win10
- 【轉】批處理命令請求以管理員身份執行
- Runas命令:能讓域使用者/普通User使用者以管理員身份執行指定程式
- 如何以管理員身份執行cmd
- win7如何設定一直以管理員身份執行Win7
- Win10系統中怎麼在工作管理員中以管理員身份執行新任務Win10
- Win10系統下以管理員身份執行cmd命令的方法Win10
- win10管理員身份執行在哪裡 win10管理員身份執行設定的方法Win10
- Win10 1909系統下怎樣以管理員身份執行PowerShellWin10
- win10管理員身份無法執行怎麼辦_win10管理員身份無法執行怎麼解決Win10
- 自動部署tomcat,並以普通使用者身份執行 for centos6TomcatCentOS
- Win10系統執行bat批處理檔案提示檔案以管理員身份執行如何解決Win10BAT
- C#多執行緒學習(四) 多執行緒的自動管理(執行緒池)C#執行緒
- C# 多執行緒學習(4) :多執行緒的自動管理(執行緒池)C#執行緒
- Win10系統右鍵選單沒有“以管理員身份執行”選項如何解決Win10
- win10找不到管理員身份執行 win10管理員許可權開啟Win10
- VS2015中使用管理員身份執行程式行程
- Python給exe新增以管理員執行的屬性Python
- C#多執行緒學習(五) 多執行緒的自動管理(定時器)C#執行緒定時器
- C# 多執行緒學習(5) :多執行緒的自動管理(定時器)C#執行緒定時器
- C#執行緒同步自動重置事件——AutoResetEventC#執行緒事件
- 在PHP中以root身份執行外部命令(轉)PHP
- Discuz非創始人管理員程式碼執行
- c# 自動更新程式C#
- C#多執行緒程式設計(1):執行緒的啟動C#執行緒程式設計
- Windows自動使用管理員許可權執行bat批處理,手動選擇啟停服務WindowsBAT
- 《自寫原創程式,讓vim執行c/c++》薦C++
- 【sudo】配置sudoers實現oracle使用者以root身份執行命令Oracle
- Linux管理指令碼之自動執行指令碼Linux指令碼
- 在這個程式中,我想讓Weblogic7 自動執行Servelt檔案。Web