Question:
.NET application developers often face challenges to capture output generated by capture console applications when integrated console applications. How to capture this output in real time without dependence on file -based methods?
Solution:
Use the attribute to redirect the output of the console application to the flow in the .NET application. Implement:
ProcessStartInfo.RedirectStandardOutput
The following C#code fragments demonstrate how to call the console application and capture its output:
Explanation:
Storage information about executable files to be executed.
<code class="language-csharp">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(); string output = compiler.StandardOutput.ReadToEnd(); Console.WriteLine(output); compiler.WaitForExit();</code>
Set and attributes to specify the console application to be called.
Disable to better control the process.ProcessStartInfo
FileName
Read the flow until the end of it, capture all the output of the console application. Arguments
UseShellExecute
The above is the detailed content of How to Capture Real-time Console Output from .NET Applications in C#?. For more information, please follow other related articles on the PHP Chinese website!