重定向一个进程对象的标准输出到另一个标准输入 [英] Redirecting stdout of one process object to stdin of another

查看:168
本文介绍了重定向一个进程对象的标准输出到另一个标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置两个外部可执行文件从哪儿第一标准输出被路由到标准输入从第二个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?

我知道该怎么办好通过使用Process对象外部程序,但我不认为做这样的事情myprogram1 - 一些-options | myprogram2 - 一些-options的一种方式。我还需要搭上第二个程序(在本例myprogram2)的标准输出

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).

在PHP中我只是这样做:

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);

和$管[1]是从链中的最后一个程序的标准输出。有没有办法在C#中做到这一点?

And $pipes[1] would be the stdout from the last program in the chain. Is there a way to accomplish this in c#?

推荐答案

下面是一个进程的标准输出接线到标准的一个基本的例子。另一个输入

Here's a basic example of wiring the standard output of one process to the standard input of another.

Process out = new Process("program1.exe", "-some -options");
Process in = new Process("program2.exe", "-some -options");

out.UseShellExecute = false;

out.RedirectStandardOutput = true;
in.RedirectStandardInput = true;

using(StreamReader sr = new StreamReader(out.StandardOutput))
using(StreamWriter sw = new StreamWriter(in.StandardInput))
{
  string line;
  while((line = sr.ReadLine()) != null)
  {
    sw.WriteLine(line);
  }
}

这篇关于重定向一个进程对象的标准输出到另一个标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆