Redirecting stdout of one process object to stdin of another

后端 未结 4 756
谎友^
谎友^ 2020-12-15 01:42

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 pro

4条回答
  •  一向
    一向 (楼主)
    2020-12-15 02:25

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

提交回复
热议问题