Running Batch Script from C#

Here is another example code to run batch script from C#


public void RunBatchScript(string batchPath, string arguments)
{
Process process = new Process();
process.StartInfo.FileName = batchPath;
process.StartInfo.Arguments = arguments;

process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;


process.OutputDataReceived += new DataReceivedEventHandler(writeStdOutStreamInfo);
process.ErrorDataReceived += new DataReceivedEventHandler(writeStdErrStreamInfo);

process.Start();

process.BeginErrorReadLine();
process.BeginOutputReadLine();

// Wait for the process to end
while (!process.HasExited)
{
Thread.Sleep(500); // sleep
}
Console.ReadKey();
}
public void writeStdOutStreamInfo(object sender,DataReceivedEventArgs e)
{
string s = e.Data;
Console.WriteLine(s);
}
public void writeStdErrStreamInfo(object sender, DataReceivedEventArgs e)
{
string s = e.Data;
Console.WriteLine(s);
}

Using there event handlers we will get the standardoutput and standarderror asynchronously in the command prompt

: