问题描述
我编写了一个通知程序,它与 WebSocket 上的 Windows 服务进行通信。此通知程序没有管理员权限,但单击按钮可启动另一个程序来启动和停止服务。这个程序应该以管理员权限运行,它运行了一段时间非常好,但不知何故不能了。
这是我的代码片段:
using (var process = new Process())
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = $".\\ServiceControl.exe";
process.StartInfo.CreateNowindow = true;
process.StartInfo.Arguments = $"-s {ServiceName} -start";
process.StartInfo.Verb = "runas";
process.Start();
}
例外情况如下:
System.ComponentModel.Win32Exception: "Der angeforderte Vorgang erfordert erhöhte Rechte"
当时它显示了一个窗口,我可以在其中接受管理员权限,但不再接受。
解决方法
尝试添加这一行
process.StartInfo.UseShellExecute = true;
开
using (var process = new Process())
{
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = $".\\ServiceControl.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = $"-s {ServiceName} -start";
process.StartInfo.Verb = "runas";
process.Start();
}