我正在开始一个代码类似于以下代码的流程:
// 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)
{
}
我遇到的问题是DataReceivedEventArgs
对象具有Data
属性,这是一个字符串。我需要读取标准输出数据作为二进制数据。我猜测没有办法将字符串数据恢复到适当的二进制数据中,因此任何使用不同方法接收二进制数据的建议都会很棒。
答案 0 :(得分:2)
答案 1 :(得分:0)
使用CliWrap,您可以执行以下操作:
var output = new MemoryStream(); // a stream, but CliWrap supports other targets too
var cmd = Cli.Wrap("app.exe").WithArguments("foo bar") | output;
await cmd.ExecuteAsync();