使用ProcessStartInfo重定向标准输出并提示输入UAC [英] Redirect standard output and prompt for UAC with ProcessStartInfo
问题描述
我正在开发针对.NET 3.0的WPF应用程序。我需要调用一个需要管理特权的exe。我可以使用以下命令让UAC提示许可:
I'm working on a WPF application targeting .NET 3.0. I need to call an exe which requires administrative privileges. I can get the UAC to prompt for permission by using something like:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Verb = "runas";
startInfo.UseShellExecute = true;
startInfo.FileName = "target.exe";
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
我的问题是我需要重定向标准输出,并使用 UseShellExecute进行重定向= true
会导致异常,指出:
My problem is I need to redirect standard output, and doing so with UseShellExecute = true
results in an exception stating that:
Process对象的UseShellExecute属性必须设置为false
以便重定向IO流
The Process object must have the UseShellExecute property set to false in order to redirect IO streams
但是,将其设置为false会导致UAC不提示许可,异常说明:
However, setting it to false results in the UAC not prompting for permission and I get an exception stating:
请求的操作需要海拔高度
The requested operation requires elevation
如何重定向标准输出并提示输入UAC?
How can I redirect standard output and prompt for UAC?
我见过类似的问题,但是解决方案是使用app.manifest授予我的应用程序管理权限。由于要求,我无法执行此操作。
I have seen this similar question, however the solution is to use the app.manifest to give my application administrative privileges. This is something I cannot do due to requirements.
推荐答案
UseShellExecute
必须设置为 false
重定向IO,设置为 true
使用动词
属性。所以你不能。
UseShellExecute
must be set to false
to redirect IO, and to true
to use the Verb
property. So you can't.
但是这篇文章似乎很神奇,尽管我尚未对其进行测试。
But this article seems do the magic, although I haven't tested it.
它是用C ++编写的,但是包装API可以使用 DllImport
轻松地创建为从C#调用。
It's written in C++, but a wrapper API can easily be created to be called from C# by using DllImport
.
注意:如果要在两个程序之间传递数据并可以访问目标程序的源代码,则可以轻松地重新设计应用程序以使用命名管道
而不是重定向标准I。 / O。
Note: If you want to pass data between the two programs and have access to the target program's source code, you can easily re-design you application to use Named Pipes
instead of redirecting standard I/O.
这篇关于使用ProcessStartInfo重定向标准输出并提示输入UAC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!