Friday, September 25, 2015

Using the ProcessRunner class

 


 

Getting from here as notepad http://csharptest.net/319/using-the-processrunner-class/

Let’s face it, using the System.Diagnostics.Process to run a process and capture the output is not as easy as it should be. People often get it wrong without realizing it. Adding to the complexities can be reading both the standard output as well as the standard error stream. To put this to bed once and for all here is the ‘right’ way all wrapped up in a helper class ProcessRunner.
Usage Example:
using CSharpTest.Net.Processes;
partial class Program
{
static int Main(string[] args)
{
 
string path = "C:\\MyProject";
ProcessRunner run = new ProcessRunner("svn.exe", "update", "{0}");
run.OutputReceived += new ProcessOutputEventHandler(run_OutputReceived);
return run.RunFormatArgs(path);
}
 
static void run_OutputReceived(object sender, ProcessOutputEventArgs args)
{
Console.WriteLine("{0}: {1}", args.Error ? "Error" : "Output", args.Data);
}
}