简体   繁体   English

从其他 c# 控制台应用程序运行 c# 控制台应用程序

[英]Run c# console app from other c# console app

I want to run a console application (B) from another console application (A).我想从另一个控制台应用程序 (A) 运行控制台应用程序 (B)。 But I want to run the console application (B) from another console window and the original window (from console application A) close.但我想从另一个控制台 window 和原始 window (来自控制台应用程序 A)关闭运行控制台应用程序(B)。

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.我试着用谷歌搜索,但似乎没有人遇到同样的问题我是 c# 的新手,我是自学成才,不太了解。

Also, I don't know how to start any application by code.另外,我不知道如何通过代码启动任何应用程序。

Thanks for the help and sorry for my English:D感谢您的帮助,对不起我的英语:D

Sample from MSDN来自MSDN的示例

// Start the child process.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "ConsoleApplication.exe"; // Need full path of application
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

I want to run a console application (B) from another console application (A).我想从另一个控制台应用程序 (A) 运行控制台应用程序 (B)。 But I want to run the console application (B) from another console window and the original window (from console application A) close.但我想从另一个控制台 window 和原始 window (来自控制台应用程序 A)关闭运行控制台应用程序(B)。

The standard Process.Start() call should do the job.标准Process.Start()调用应该可以完成这项工作。

Console App A:控制台应用程序 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:控制台应用程序 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.但是根据控制台应用程序“A”的启动方式,您会看到不同的结果。 If you double click on "A" from File Explorer, then the console window for "A" will close and you'll only see "B".如果您从文件资源管理器中双击“A”,则“A”的控制台 window 将关闭,您只会看到“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.如果您已经在命令提示符下并启动“A”,那么该控制台将保持打开状态,您会看到它和“B”一起出现。 Both scenarios are shown below.这两种情况如下所示。

Output when "A" is started from File Explorer: Output 从文件资源管理器启动“A”时:

在此处输入图像描述

Output when "A" is started from an existing Command Prompt: Output 从现有命令提示符启动“A”时:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM