Getting Binary Data from StandardOutput

2020-04-14 12:54发布

问题:

I'm starting a process with code similar to that below:

// 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)
{
}

The issue I'm running into is that the DataReceivedEventArgs object has a Data property which is a string. I need to read the standard output data as the binary data it is. I'm guessing there's no way to get the string data back into it's appropriate binary data, so any suggestions on using a different method for receiving the binary data would be great.

回答1:

Bradley Grainger who made a comment on the question was right. The event handlers don't support retrieving binary data from standard out. Had to switch over to using a main loop and pulling data from standard out using the read functions.