tags:

views:

986

answers:

1

I have to use an other application (console) to pass some parameter to this program and inside my C# program get the output of that program. I would like not to see the console (all invisible to the user). How can I do that?

+12  A: 
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("YOUPROGRAM_CONSOLE.exe" );
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();

StreamReader myStreamReader = myProcess.StandardOutput;
string myString = myStreamReader.ReadLine();
Console.WriteLine(myString);
myProcess.Close();

Source : MSDN

Edited: If you require to get the Error Message you will need to use Async operation. You can use asynchronous read operations to avoid these dependencies and their deadlock potential. Alternately, you can avoid the deadlock condition by creating two threads and reading the output of each stream on a separate thread.

Daok
You'll want to enable "RedirectStandardError" too, and read that stream,In case your sub process generates error messages.
gimel
I added some information. IN the MSDN it says that it require multiple thread to avoid deadlock.
Daok