40

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.

Dawid Ohia
  • 16,129
  • 24
  • 81
  • 95

6 Answers6

40

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
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    On Windows 10 where cmd is the Windows Server 2008 Build Environment, this does no longer work, the new application immediately opens a new window. – Georg Sep 22 '16 at 15:17
9

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
  • 114,645
  • 34
  • 221
  • 317
7

I was able to run the program 'B' as part of the same command window by calling the following configuration:

ConsoleColor color = Console.ForegroundColor;
ProcessStartInfo startinfo = new ProcessStartInfo(nameProgramB);
startinfo.CreateNoWindow = false;
startinfo.UseShellExecute = false;
Process p = Process.Start(startinfo);
p.WaitForExit();
Console.ForegroundColor = color;

this way, both programs run seamlesly like they were one single program. 'nameProgramB' is the name to program 'B'. Hope this helps.

Axel
  • 3,331
  • 11
  • 35
  • 58
Chesare
  • 333
  • 4
  • 8
  • Thank you very much Chesare, it works fantastic. BTW, it seems that we don't need to set `.CreateNoWindow` to false, since it's already false by default. The critical property to set here, is `.UseShellExecute`, since its default is true, and it must be false for the second program to run in the existing window and not in a new one. – spaceman May 28 '20 at 20:45
4

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
  • 31,970
  • 14
  • 96
  • 151
  • 1
    Hi Paul. Altho 10 years passed since this answer, I checked it now, and `Process.Start("other_console_program")` creates a new (second) Console window. The only way to make it not create a second one, is to set create a ProcessStartInfo object, and then set its .UseShellExecute property to false. Like in Chesare's answer. – spaceman May 28 '20 at 20:42
1

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
  • 33,512
  • 4
  • 61
  • 92
1

you can "wrap" the old console app with the new one by including it in your references and starting it off by calling whatever method is called in the run method of the program class

Sjors Miltenburg
  • 2,540
  • 4
  • 33
  • 60