c# redirect (pipe) process output to another process

前端 未结 2 1506
别跟我提以往
别跟我提以往 2020-12-05 08:17

I am trying to run a process in c# using the Process class.

Process p1  = new process();
p1.startinfo.filename =  \"xyz.exe\";
p1.startinfo.arguments = //i a         


        
相关标签:
2条回答
  • 2020-12-05 08:47

    You can use CliWrap to make piping really easy, without the overhead of a console, and also completely x-platform:

    var output = File.Create("output.txt"); // stream is just one of the options
    
    var cmd = Cli.Wrap("xyz.exe") | output;
    await cmd.ExecuteAsync();
    
    0 讨论(0)
  • 2020-12-05 09:02

    The much easier way would be to do just use cmd as your process.

    Process test = new Process();
    test.StartInfo.FileName = "cmd";
    test.StartInfo.Arguments = @"/C ""echo testing | grep test""";
    test.Start();
    

    You can capture the output or whatever else you want like any normal process then. This was just a quick test I built, but it works outputting testing to the console so I would expect this would work for anything else you plan on doing with the piping. If you want the command to stay open then use /K instead of /C and the window will not close once the process finishes.

    0 讨论(0)
提交回复
热议问题