将一个进程对象的标准输出重定向到另一个进程对象的标准输入
如何设置两个外部可执行文件从 ac# 应用程序运行,其中第一个应用程序的标准输出从第二个应用程序路由到标准输入?
我知道如何使用 Process 对象来运行外部程序,但我没有看到类似“myprogram1 -some -options | myprogram2 -some -options”之类的方法。我还需要捕获第二个程序的标准输出(示例中为 myprogram2)。
在 PHP 中,我会这样做:
$descriptorspec = array(
1 => array("pipe", "w"), // stdout
);
$this->command_process_resource = proc_open("myprogram1 -some -options | myprogram2 -some -options", $descriptorspec, $pipes);
$pipes[1] 将是链中最后一个程序的标准输出。有没有办法在 C# 中实现这一点?
How can I set up two external executables to run from a c# application where stdout from the first is routed to stdin from the second?
I know how to run external programs by using the Process object, but I don't see a way of doing something like "myprogram1 -some -options | myprogram2 -some -options". I'll also need to catch the stdout of the second program (myprogram2 in the example).
In PHP I would just do this:
$descriptorspec = array(
1 => array("pipe", "w"), // stdout
);
$this->command_process_resource = proc_open("myprogram1 -some -options | myprogram2 -some -options", $descriptorspec, $pipes);
And $pipes[1] would be the stdout from the last program in the chain. Is there a way to accomplish this in c#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是将一个进程的标准输出连接到另一个进程的标准输入的基本示例。
Here's a basic example of wiring the standard output of one process to the standard input of another.
您可以使用 System.Diagnostics.Process 类创建 2 个外部进程,并通过 StandardInput 和 StandardOutput 属性将输入和输出粘在一起。
You could use the System.Diagnostics.Process class to create the 2 external processes and stick the in and outs together via the StandardInput and StandardOutput properties.
使用 System.Diagnostics.Process 启动每个进程,并在第二个进程中将 RedirectStandardOutput 设置为 true,并将第一个 RedirectStandardInput 设置为 true。最后将第一个的 StandardInput 设置为第二个的 StandardOutput 。您需要使用 ProcessStartInfo 来启动每个进程。
以下是其中一个重定向示例。
Use System.Diagnostics.Process to start each process, and in the second process set the RedirectStandardOutput to true, and the in the first RedirectStandardInput true. Finally set the StandardInput of the first to the StandardOutput of the second . You'll need to use a ProcessStartInfo to start each process.
Here is an example of one of the redirections.