提升权限不适用于UseShellExecute=false.我接下来做:varinfo=newProcessStartInfo(Assembly.GetEntryAssembly().Location){UseShellExecute=true,//!动词="runas",};varprocess=newProcess{StartInfo=info};过程.开始();这有效:varidentity=newWindowsPrincipal(WindowsIdentity.GetCurrent());identity.IsInRole(WindowsBuiltInRole.Administrator);//返回true但UseShellExecute=true创建了一个新窗口,我也无法重定向输出。所以下次我这样做:varinfo=newProcessStartInfo(Assembly.GetEntryAssembly().Location){RedirectStandardError=true,RedirectStandardOutput=true,UseShellExecute=false,//!动词="runas"};varprocess=newProcess{EnableRaisingEvents=true,StartInfo=info};DataReceivedEventHandleractionWrite=(sender,e)=>{Console.WriteLine(e.Data);};process.ErrorDataReceived+=actionWrite;process.OutputDataReceived+=actionWrite;过程.开始();process.BeginOutputReadLine();process.BeginErrorReadLine();过程.WaitForExit();这不会提升权限,上面的代码返回false。为什么??ProcessStartInfo.Verb只有在进程被ShellExecuteEx()启动时才会生效。这需要UseShellExecute=true。重定向I/O和隐藏窗口只有在进程由CreateProcess()启动时才有效。这需要UseShellExecute=false。好吧,这就是它不起作用的原因。不确定是否有意禁用启动绕过UAC的隐藏进程。也许。很可能。检查此Q+A以获得显示UAC提升提示所需的清单。在我的例子中,一旦提升的子进程完成,输出就可用。这是我想出的解决方案。它使用一个临时文件:varoutput=Path.GetTempFileName();varprocess=Process.Start(newProcessStartInfo{FileName="cmd",Arguments="/cechoI'manadmin>"+output,//重定向到临时文件Verb="runas",//UACpromptUseShellExecute=真的,});过程.WaitForExit();字符串res=File.ReadAllText(output);//对输出做一些事情File.Delete(output);检查这个答案。这似乎提供了一种解决方法。但是,当您可以访问子进程的源代码时,我建议您尝试其他方法,例如命名管道。以上为C#学习教程:ElevatingpermissionsdoesnotapplytoallthesharecontentbyUseShellExecute=false。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。涉及侵权,请点击维权联系管理员删除。如需转载请注明出处:
