从 StandardOutput 获取二进制数据

Bri*_*den 4 c# process binary-data redirectstandardoutput

我正在使用类似于下面的代码开始一个过程:

// some of the flags are not needed
process.StartInfo.CreateNoWindow = true;
process.StartInfo.ErrorDialog = false;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.EnableRaisingEvents = true;
process.OutputDataReceived += process_OutputDataReceived;
process.ErrorDataReceived += process_OutputDataReceived;
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();

void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
}

void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是该DataReceivedEventArgs对象有Data一个字符串属性。我需要将标准输出数据读取为二进制数据。我猜想没有办法将字符串数据恢复为适当的二进制数据,因此任何有关使用不同方法接收二进制数据的建议都会很棒。

Bri*_*den 5

布拉德利·格兰杰对这个问题的评论是正确的。事件处理程序不支持从标准输出检索二进制数据。必须切换到使用主循环并使用读取函数从标准输出中提取数据。

  • 刚刚发现困难的方法。 (2认同)