4dots Software
CODE HELP BLOG
// we create the process
Process myproc = new Process();
myproc.StartInfo.FileName = "myprogram";
myproc.StartInfo.CreateNoWindow = true;
myproc.StartInfo.UseShellExecute = false;
myproc.StartInfo.RedirectStandardOutput = true;
myproc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
// we start it
myproc.Start();
string line=null;
string myproc_output="";
// we read from the standard output stream but if no output is written from the process it will freeze here
while ((line=myproc.StandardOutput.ReadLine())!=null)
{
myproc_output+=line;
}
// variable that holds the output of the process
private string myproc_output="";
private void ReadProcessOutput()
{
// we create the process
Process myproc = new Process();
myproc.StartInfo.FileName = "myprogram";
myproc.StartInfo.CreateNoWindow = true;
myproc.StartInfo.UseShellExecute = false;
myproc.StartInfo.RedirectStandardOutput = true;
myproc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
// we add the OutputDataReceived event handler
myproc.OutputDataReceived += myproc_OutputDataReceived;
// we start the process
myproc.Start();
// we start reading from the Standard Output asynchronously
myproc.BeginOutputReadLine();
// we wait till the process exits
myproc.WaitForExit();
myproc.Close();
Console.WriteLine("myproc output="+myproc_output);
}
void myproc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
// data was received - we add it to the string variable
myproc_output+=e.Data;
}