提升权限不适用于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);
检查这个答案 。
这似乎提供了一种解决方法。 但是,当您有权访问子进程的源代码时,我建议尝试其他方法,如命名管道 。
上述就是C#学习教程:提升权限不适用于UseShellExecute = false分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1042248.html