c# 管理员权限启动外部程序
1.目前程序启动的时候已经有管理员权限的判断了。但是启动外部程序的时候仍然提示需要提升权限
配置文件app.manifest也改过了`
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />`
2.启动权限为管理员
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
//Application.SetCompatibleTextRenderingDefault(false);
/**
* 当前用户是管理员的时候,直接启动应用程序
* 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
*/
//获得当前登录的Windows用户标示
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
//如果是管理员,则直接运行
App app = new App();
app.Run(new Login());
}
else
{
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "runas";
try
{
System.Diagnostics.Process.Start(startInfo);
}
catch
{
return;
}
//退出
Application.Exit();`
3.`启动程序代码
p.StartInfo.FileName = startPath;
p.StartInfo.Arguments = "1" ;//启动参数
p.StartInfo.Verb = "runas";
p.StartInfo.CreateNoWindow = true; //设置置不显示示窗口
p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动进程
p.StartInfo.ErrorDialog = false;
p.Start();`
4.异常信息
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论