4

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?

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

2 Comments

I would suggest myStreamReader.ReadToEnd();, but a solid +1. ;)
@Lance May - I meant to make that change before posting. That was what MSDN originally used. I made the update.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.