我想启动一个具有提升权限但隐藏窗口的子进程(实际上是相同的控制台应用程序)。
我接下来要做的是:
var info = new ProcessStartInfo(Assembly.GetEntryAssembly().Location)
{
UseShellExecute = true, // !
Verb = "runas",
};
var process = new Process
{
StartInfo = info
};
process.Start();
这有效:
var identity = new WindowsPrincipal(WindowsIdentity.GetCurrent());
identity.IsInRole(WindowsBuiltInRole.Administrator); // returns true
但是 UseShellExecute = true
创建了一个新窗口,我也无法重定向输出。
所以当我下一步做的时候:
var info = new ProcessStartInfo(Assembly.GetEntryAssembly().Location)
{
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false, // !
Verb = "runas"
};
var process = new Process
{
EnableRaisingEvents = true,
StartInfo = info
};
DataReceivedEventHandler actionWrite = (sender, e) =>
{
Console.WriteLine(e.Data);
};
process.ErrorDataReceived += actionWrite;
process.OutputDataReceived += actionWrite;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
这不会提升权限,上面的代码会返回 false。为什么??
最佳答案
ProcessStartInfo.Verb 只有在进程由 ShellExecuteEx() 启动时才有效。这需要 UseShellExecute = true。重定向 I/O 和隐藏窗口只有在进程由 CreateProcess() 启动时才有效。这需要 UseShellExecute = false。
嗯,这就是它不起作用的原因。不确定禁止启动绕过 UAC 的隐藏进程是否是故意的。大概。 非常可能。
检查 this Q+A对于 list ,您需要显示 UAC 提升提示。
关于c# - 提升权限不适用于 UseShellExecute=false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3596259/