tags:

views:

60

answers:

2

I am looking for a c# example project of a WinForms app which redirects the output from a batch file running in background to any kind of WinForms control.

Any suggestions?

+4  A: 

I don't know if you're going to find a direct example, but it's not overly difficult. I don't have time to write all the code for you, but I can give you the code from MSDN:

Process myProcess = new Process();

ProcessStartInfo myProcessStartInfo = 
    new ProcessStartInfo("C:\\MyBatchFile.bat" );
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;

myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();

StreamReader myStreamReader = myProcess.StandardOutput;

// Read the standard output of the spawned process.
string myString = myStreamReader.ReadToEnd();

myProcess.Close();

// Now you have the output of the batch file in myString
Justin Niessner
I would suggest `myStreamReader.ReadToEnd();`, but a solid +1. ;)
Lance May
@Lance May - I meant to make that change before posting. That was what MSDN originally used. I made the update.
Justin Niessner
A: 

See this answer here.

It will show you how to redirect the output to an event. You can then take the output and put it into your win control.

David Basarab