Home >Backend Development >C++ >How to Efficiently Capture Console Application Output in .NET?
Start a console application process and capture its output in .NET. You can use the following methods:
This method uses the Event processing program to add the received output to the string. We call
<code class="language-csharp">string retMessage = string.Empty; ProcessStartInfo startInfo = new ProcessStartInfo(); Process p = new Process(); startInfo.CreateNoWindow = true; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardInput = true; startInfo.UseShellExecute = false; startInfo.Arguments = command; startInfo.FileName = exec; p.StartInfo = startInfo; p.Start(); p.OutputDataReceived += (sender, args) => retMessage += args.Data; // 开始异步输出读取。 p.BeginOutputReadLine(); p.WaitForExit(); return retMessage;</code>to start the output asynchronous reading. By default, will block until the child process exits, which allows the output to be completely captured.
OutputDataReceived
Another method of reading the output is: BeginOutputReadLine()
WaitForExit()
The above is the detailed content of How to Efficiently Capture Console Application Output in .NET?. For more information, please follow other related articles on the PHP Chinese website!