Capturing console output from a .NET application (C#)

后端 未结 8 2293
臣服心动
臣服心动 2020-11-22 02:20

How do I invoke a console application from my .NET application and capture all the output generated in the console?

(Remember, I don\'t want to save the information

8条回答
  •  一生所求
    2020-11-22 02:57

    This can be quite easily achieved using the ProcessStartInfo.RedirectStandardOutput property. A full sample is contained in the linked MSDN documentation; the only caveat is that you may have to redirect the standard error stream as well to see all output of your application.

    Process compiler = new Process();
    compiler.StartInfo.FileName = "csc.exe";
    compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
    compiler.StartInfo.UseShellExecute = false;
    compiler.StartInfo.RedirectStandardOutput = true;
    compiler.Start();    
    
    Console.WriteLine(compiler.StandardOutput.ReadToEnd());
    
    compiler.WaitForExit();
    

提交回复
热议问题