Created
March 13, 2012 12:42
-
-
Save CharlTruter/2028533 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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