简体   繁体   English

如何在进程操作期间逐行捕获进程STDOUT和STDERR。 (C#)

[英]How to capture a Processes STDOUT and STDERR line by line as they occur, during process operation. (C#)

I am going to execute a Process (lame.exe) to encode a WAV file to MP3. 我将执行一个进程(lame.exe)将WAV文件编码为MP3。

I want to process the STDOUT and STDERR of the process to display progress information. 我想处理进程的STDOUT和STDERR以显示进度信息。

Do I need to use threading? 我需要使用线程吗? I can't get my head around it. 我无法理解它。

Some simple example code would be appreciated. 一些简单的示例代码将不胜感激。

Thanks 谢谢

If running via the Process class, you can redirect the streams so you may process them. 如果通过Process类运行,则可以重定向流以便可以处理它们。 You can read from stdout or stderr synchronously or asynchronously. 您可以同步或异步读取stdout或stderr。 To enable redirecting, set the appropriate redirection properties to true for the streams you want to redirect (eg, RedirectStandardOutput ) and set UseShellExecute to false . 要启用重定向,请为要重定向的流(例如, RedirectStandardOutput )将相应的重定向属性设置为true ,并将UseShellExecute设置为false Then you can just start the process and read from the streams. 然后你就可以开始这个过程并从流中读取。 You can also feed input redirecting stdin. 您还可以输入重定向标准输入的输入。

eg, Process and print whatever the process writes to stdout synchronously 例如,处理并打印进程同步写入stdout的任何内容

var proc = new Process()
{
    StartInfo = new ProcessStartInfo(@"SomeProcess.exe")
    {
        RedirectStandardOutput = true,
        UseShellExecute = false,
    }
};
if (!proc.Start())
{
    // handle error
}
var stdout = proc.StandardOutput;
string line;
while ((line = stdout.ReadLine()) != null)
{
    // process and print
    Process(line);
    Console.WriteLine(line);
}

You should be able to listen to the STDOUT with the Process.OutputDataReceived event. 您应该能够使用Process.OutputDataReceived事件监听STDOUT。 There is an example on the MSDN page. MSDN页面上有一个示例。 There is also an Process.ErrorDataReceived event for STDERR. STDERR还有一个Process.ErrorDataReceived事件。

There is an MSDN example for this... Here is a simplified version: 一个MSDN示例 ...这是一个简化版本:

var StdOut = "";
var StdErr = "";

var stdout = new StringBuilder();
var stderr = new StringBuilder();

var psi = new ProcessStartInfo();
psi.FileName = @"something.exe";
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;

var proc = new Process();
proc.StartInfo = psi;
proc.OutputDataReceived += (sender, e) => { stdout.AppendLine(e.Data); };
proc.ErrorDataReceived += (sender, e) => { stderr.AppendLine(e.Data); };
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit(10000); // per sachin-joseph's comment

StdOut = stdout.ToString();
StdErr = stderr.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#行捕获 - C# Line In Capture 以正确的顺序捕获进程stdout和stderr - capture process stdout and stderr in the correct ordering 使用C#中断正在运行的进程后如何继续在命令行窗口中执行其他进程 - How to continue executing other processes in command line window after breaking a running process using C# C# TCPClient SocketError 10051 在读取操作期间。 适配器 IP 为空 - C# TCPClient SocketError 10051 during read operation. Adapters IP is empty 如何从C#中读取PowerShell脚本stdout和stderr - How to read PowerShell script stdout and stderr from C# 如何捕获未发送到stdout的命令行文本? - How do I capture command-line text that is not sent to stdout? C# - 尝试使用导入方法从.p8文件创建CngKey,抛出错误“编码或解码操作期间发生错误”。 - C# - Trying to create a CngKey from a .p8 file with import method, throwing error “An error occurred during encode or decode operation.” 如何使用stdin和stdout在C#和ruby进程之间进行通信 - How to communicate between C# and ruby processes using stdin and stdout C#字符串操作。 获取文件名子字符串 - C# string operation. get file name substring C#,如何在foreach循环中将字符串拆分到新行? - C#, How to split strings to new line during foreach loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM