views:

301

answers:

4

I have a C# console application (A). I want to execute other console app (B) from within app A (in synchronous manner) in such way that B uses the same command window. When B exists, A should be able to read B's exit code.

How to do that? I need only this little tip on how to run this other app in same cmd window.

+7  A: 

You can start another process using the Process.Start() call. The examples here show how to read output from other process and wait for it to finish.

Brian Rasmussen
+1  A: 

You can start another process with Process.Start - doesn't really matter if it's a console app or not. If your app is already running in a console window the newly spawned app will use that console window as well.

var proc = Process.Start( "...path to second app" );
proc.WaitForExit();
var exitCode = proc.ExitCode;

Be sure to ready the docs on the Process class as there are a variety of little nuances that may affect the way your secondary app is launched.

Paul Alexander
+5  A: 

You can use Process.Start to start the other console application.

You will need to construct the process with ProcessStartInfo.RedirectOutput set to true and UseShellExecute set to false in order to be able to utilize the output yourself.

You can then read the output using StandardOutput.ReadToEnd on the process.

Oded
A: 

Fill out a System.Diagnostics.ProcessStartInfo and pass it to Process.Start

You can WaitForExit on the resulting process, and use then use ExitCode property of the process to see the return value.

John Knoeller