Skip to content

Instantly share code, notes, and snippets.

@CharlTruter
Created March 13, 2012 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharlTruter/2028533 to your computer and use it in GitHub Desktop.
Save CharlTruter/2028533 to your computer and use it in GitHub Desktop.
void RunProcess(string path)
{
using (Process process = new Process())
{
// This sets up the output redirection
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
// Specify the path to the file here
process.StartInfo.FileName = path;
// Bind the event handler that will be fired when output is received
process.OutputDataReceived += new DataReceivedEventHandler(processOutputReceived);
// Start the process here
process.Start();
// This starts to asynchronously check for output from the process.
// This is crucial for the event handler to fire
process.BeginOutputReadLine();
// Wait for the process to finish execution
process.WaitForExit();
}
}
static void processOutputReceived(object sender, DataReceivedEventArgs e)
{
// Here you can interact with the output of the application
// In my case, I simply write out the data to the console
Console.WriteLine(e.Data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment