提升权限不适用于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。 为什么??

只有在ShellExecuteEx()启动进程时,ProcessStartInfo.Verb才会生效。 这需要UseShellExecute = true。 重定向I / O并隐藏窗口只有在CreateProcess()启动进程时才能工作。 这需要UseShellExecute = false。

嗯,这就是为什么它不起作用。 不确定是否禁止启动绕过UAC的隐藏进程是故意的。 大概。 可能。

检查此Q + A以获取显示UAC提升提示所需的清单。

在我的情况下,一旦提升的子进程完成就可以获得输出。 这是我提出的解决方案。 它使用临时文件:

 var output = Path.GetTempFileName(); var process = Process.Start(new ProcessStartInfo { FileName = "cmd", Arguments = "/c echo I'm an admin > " + output, // redirect to temp file Verb = "runas", // UAC prompt UseShellExecute = true, }); process.WaitForExit(); string res = File.ReadAllText(output); // do something with the output File.Delete(output); 

检查这个答案 。

这似乎提供了一种解决方法。 但是,当您有权访问子进程的源代码时,我建议尝试其他方法,如命名管道 。