C# 让程序自动以管理员身份运行


无意中遇到需要以管理员方式运行的程序,最终找到如下的解决方法:

在程序中加入MANIFEST资源文件

首先打开Vs工程,看在Properties下是否有app.manifest这个文件;

如没有,右击工程在菜单中选择“Properties”,出现界面如下图:

选中"Security",在界面中勾选"Enable ClickOnce Security Settings"后,在Properties下就有自动生成app.manifest文件。

打开app.manifest文件,找到如下代码:

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

 将其修改为:

  1. <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />  

 然后在"Security"中再勾去"Enable ClickOnce Security Settings"后,重新编译就可以了。

原文地址:https://www.cnblogs.com/caigen029/p/3701520.html