I have been trying to get this to work with several tutorials/answers here, but unfortunately have not been able to do so.
I want to do is to execute a process, capture its DefaultOutput and add it to a byte array.
What I got so far is:
private void startProcess(string path, string arguments)
{
Process p = new Process();
p.StartInfo.FileName = path;
p.StartInfo.Arguments = arguments;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.OutputDataReceived += p_OutputDataReceived;
p.EnableRaisingEvents = true;
p.Start();
p.BeginErrorReadLine();
p.BeginOutputReadLine();
p.WaitForExit();
}
void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
string str = e.Data;
// what goes here?!
}
My problem now is: How do I add this data to a (growing) byte array as it comes in or is there another datatype better suited for this purpose?
Also I am not sure where to declare this destination byte array, preferably it would be somewhere in the startProcess Method so I can continue to work with the data after the process Exited, but how could I pass this to p_OutputDataReceived?
Thanks!
You might try a memorystream; it really does what you’re trying to do.