单个控制台应用程序中的多个控制台 [英] Multiple Consoles in a Single Console Application
问题描述
我创建了一个C#项目,其中包含多个控制台应用程序。现在我的问题是:运行一个应用程序时是否可以显示多个控制台?是的,如何?
I have created a C# Project which has multiple console applications in it. Now my question is: Is it possible to display multiple consoles when I run one application? if yes, how?
我说,我有测试应用程序,这是主要应用程序。我还有另外两个控制台应用程序,分别是 ABC
和 XYZ
。现在,当我运行测试应用程序时,应同时显示应用程序ABC和XYZ的控制台。
Lets say, I have a Test Application, which is the main application. I have another two Console applications say, ABC
and XYZ
. Now, when i run the Test Application, the console of both applications ABC and XYZ should appear.
我编写了以下代码:
Console.WriteLine("\n\t Calling EXE...");
Process myProcess = new Process();
string Exepath = System.IO.Directory.GetCurrentDirectory() + "\\exe\\ABCApplication.exe";
try
{
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = Exepath;
myProcess.StartInfo.CreateNoWindow = false;
myProcess.Start();
}
catch (Exception ex)
{
CreateLogFile();
}
推荐答案
下面是一个简单的例子可以完成...显然,可以根据自己的喜好调整路径,还有其他几种方法:
Here's a quick example of what can be done... obviously, adjust paths to your liking and there are a few other ways:
预览:
代码:
using (var process1 = new Process())
{
process1.StartInfo.FileName = @"..\..\..\ConsoleApp1\bin\Debug\ConsoleApp1.exe";
process1.Start();
}
using (var process2 = new Process())
{
process2.StartInfo.FileName = @"..\..\..\ConsoleApp2\bin\Debug\ConsoleApp2.exe";
process2.Start();
}
Console.WriteLine("MainApp");
Console.ReadKey();
这是一个快速设置,可以并且应该进行很多调整(异常处理等,等)。不过,它应该可以帮助您入门。
This was a quick setup and many things can be and should be adjusted (exception handling, etc., etc., etc.). It should get you started, though.
这篇关于单个控制台应用程序中的多个控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!