C# 调用Process.Start 请求的操作需要提升解决方案


运行CSFrameworkV5.Main.exe主程序时提示:“请求的操作需要提升。”

解决方案:C#实现以管理员身份权限运行程序2种方式
C#实现以管理员身份权限运行程序 - 方式1:第一种方式简单便捷,推荐!
1. 通过添加应用程序清单文件(app.manifest):
右键你的项目 -> 添加 -> 新建项 -> “应用程序清单文件”。这个应用程序清单文件在对话框最下方,需要一直拉到最下面才能找到。

添加后,打开这个文件,然后找到大约18行或者19行的样子,将level后面的字符串修改如下:
XML Code:
<requestedExecutionLevel level="asInvoker" uiAccess="false" /> //原代码
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> //修改后
<requestedExecutionLevel level="asInvoker" uiAccess="false" /> //原代码
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> //修改后
app.manifest文件:
XML Code:
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<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 清单选项
如果想要更改 Windows 用户帐户控制级别,请使用下面方法:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
-->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<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 清单选项
如果想要更改 Windows 用户帐户控制级别,请使用下面方法:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
-->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
//来源:C/S框架网 | www.csframework.com | QQ:23404761
然后右键项目,点击属性,将应用程序右侧的资源里面的清单,选择为你刚刚添加的文件app.manifest

重新生成项目即可。
最后,双击打开exe程序,就会提示你是否以管理员身份运行,如图:

2. 编写代码以管理员身份运行
C# Code:
class Program
{
static void Main(string[] args)
{
/**
* 当前用户是管理员的时候,直接启动应用程序
* 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
*/
//获得当前登录的Windows用户标示
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
//创建Windows用户主题
Application.EnableVisualStyles();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
//如果是管理员,则直接运行
Application.EnableVisualStyles();
Application.Run(new Form1());//这里是运行了一个form的窗口
}
else
{
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//设置运行文件
startInfo.FileName = Application.ExecutablePath;
//设置启动参数
startInfo.Arguments = String.Join(" ", args);
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "runas";
//如果不是管理员,则启动UAC
System.Diagnostics.Process.Start(startInfo);
//退出
Application.Exit();
}
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
{
static void Main(string[] args)
{
/**
* 当前用户是管理员的时候,直接启动应用程序
* 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
*/
//获得当前登录的Windows用户标示
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
//创建Windows用户主题
Application.EnableVisualStyles();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
//如果是管理员,则直接运行
Application.EnableVisualStyles();
Application.Run(new Form1());//这里是运行了一个form的窗口
}
else
{
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//设置运行文件
startInfo.FileName = Application.ExecutablePath;
//设置启动参数
startInfo.Arguments = String.Join(" ", args);
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "runas";
//如果不是管理员,则启动UAC
System.Diagnostics.Process.Start(startInfo);
//退出
Application.Exit();
}
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761

扫一扫加作者微信


版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网