简体   繁体   English

从WPF应用程序启动控制台应用程序

[英]launch console application from WPF application

I would like to know if it is possible to launch a console application from a WPF application. 我想知道是否可以从WPF应用程序启动控制台应用程序。

In my console application I have added a class called Reports. 在控制台应用程序中,我添加了一个名为Reports的类。 In my WPF application I create a Reports object and call one of its public methods. 在我的WPF应用程序中,我创建一个Reports对象并调用其公共方法之一。

The method it calls contains the code below. 它调用的方法包含以下代码。 The issue is that the code doesn't wait for the user to enter any input so it crashes in the ValidateUserInput. 问题在于该代码不会等待用户输入任何输入,因此会在ValidateUserInput中崩溃。 I also do not see any console application actually load. 我也看不到任何控制台应用程序实际加载。 I can't see how to call the Main method of the Program class in my console application. 我在控制台应用程序中看不到如何调用Program类的Main方法。 As you can probably tell I'm pretty lost. 如您所知,我迷路了。

Console.WriteLine("*** Running Report ***");                
Console.WriteLine("User enter something:");
string myStr = Console.ReadLine();
ValidateUserInput(myStr);

Since you are running WPF application, all Console outputs go to output window of Visual Studio if running in debug mode. 由于您正在运行WPF应用程序,因此,如果在调试模式下运行,则所有控制台输出都将转到Visual Studio的输出窗口。

For actually launching Console application you have to manually open Console window and close it after use . 要实际启动Console应用程序,您必须手动打开Console窗口并在使用后将其关闭 Add following methods in your class: 在您的课程中添加以下方法:

[DllImport("Kernel32")]
public static extern void AllocConsole();

[DllImport("Kernel32", SetLastError = true)]
public static extern void FreeConsole();

Now, use methods like this: 现在,使用如下方法:

AllocConsole();
report.ConsoleMethod() // Method to console application.
FreeConsole();

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

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