Warm tip: This article is reproduced from serverfault.com, please click

Run c# console app from other c# console app

发布于 2020-11-28 14:19:22

I want to run a console application (B) from another console application (A). But I want to run the console application (B) from another console window and the original window (from console application A) close.

I tried to google it but no one seems to have the same problem I am new to c# and I am self-taught and don't know about it much.

Also, I don't know how to start any application by code.

Thanks for the help and sorry for my English :D

Questioner
Pars
Viewed
0
Idle_Mind 2020-11-28 23:58:34

I want to run a console application (B) from another console application (A). But I want to run the console application (B) from another console window and the original window (from console application A) close.

The standard Process.Start() call should do the job.

Console App A:

public static void Main(string[] args)
{
    Console.WriteLine("This is Console App 'A'...");

    string fullPathToB = @"C:\Users\mikes\Documents\Visual Studio 2017\Projects\CS_Console_Scratch2\CS_Console_Scratch2\bin\Debug\ConsoleAppB";
    Process.Start(fullPathToB);

    // this console app will close when we hit the end bracket for Main()
}

Console App B:

public static void Main(string[] args)
{
    Console.WriteLine("This is Console App 'B'...");

    Console.WriteLine("Press Enter to Quit");
    Console.ReadLine();
}

But you'll see different results depending on how Console App "A" was started. If you double click on "A" from File Explorer, then the console window for "A" will close and you'll only see "B".

If you are already at a command prompt and start "A", then that console will stay open and you'll see it and "B" together. Both scenarios are shown below.

Output when "A" is started from File Explorer:

enter image description here

Output when "A" is started from an existing Command Prompt:

enter image description here