问题描述
我要开始一个子进程(实际上是相同的,控制台应用程序),以提升的权限,但隐藏的窗口。
I want to start a child process (indeed the same, console app) with elevated privileges but with hidden window.
我接下来做:
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
创建一个新的窗口,我也我不能重定向输出。
But UseShellExecute = true
creates a new window and I also I can't redirect output.
所以下次当我这样做:
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();
这不提升权限和code以上返回false。为什么?
This doesn't elevate privileges and code above returns false. Why??
推荐答案
如果该过程由开始的ShellExecuteEx将ProcessStartInfo.Verb只能有一个效果()。这就要求UseShellExecute = TRUE。 I / O重定向和隐藏窗口,如果过程由CreateProcess的启动只能工作()。这就要求UseShellExecute =假的。
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.
好吧,这就是为什么它不工作。不知道是否禁止启动隐藏进程绕过UAC是故意的。大概。的非常的可能。
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.
检查this螺纹的清单,你需要显示UAC提升提示。
Check this thread for the manifest you need to display the UAC elevation prompt.
这篇关于提升权限不UseShellExecute工作= FALSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!