UseShellExecute=false 时无法提升权限
我想启动一个具有提升权限但具有隐藏窗口的子进程(实际上是相同的控制台应用程序)。
我执行下一步:
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。为什么??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ProcessStartInfo.Verb 仅当进程由 ShellExecuteEx() 启动时才有效。这需要 UseShellExecute = true。仅当进程由 CreateProcess() 启动时,重定向 I/O 和隐藏窗口才有效。这需要 UseShellExecute = false。
嗯,这就是为什么它不起作用。不确定禁止启动绕过 UAC 的隐藏进程是否是有意为之。大概。 非常可能。
检查此问答< /a> 对于清单,您需要显示 UAC 提升提示。
ProcessStartInfo.Verb will only have an effect if the process is started by ShellExecuteEx(). Which requires UseShellExecute = true. Redirecting I/O and hiding the window can only work if the process is started by CreateProcess(). Which requires UseShellExecute = false.
Well, that's why it doesn't work. Not sure if forbidding to start a hidden process that bypasses UAC was intentional. Probably. Very probably.
Check this Q+A for the manifest you need to display the UAC elevation prompt.
就我而言,一旦提升的子进程完成,就可以获取输出。这是我想出的解决方案。它使用一个临时文件:
In my case, it was ok to get the outputs once the elevated child process is done. Here's the solution I came up. It uses a temporary file :
检查此答案。
这似乎提供了一种解决方法。但我建议您在有权访问时尝试其他方法,例如 命名管道到子进程的源代码。
Check this answer.
This seems to provide a workaround. But I recommend to try other methods like Named Pipes when you have access to source code of the child process.