简体   繁体   中英

launch console application from WPF application

I would like to know if it is possible to launch a console application from a WPF application.

In my console application I have added a class called Reports. In my WPF application I create a Reports object and call one of its public methods.

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. 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. 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.

For actually launching Console application you have to manually open Console window and close it after use . 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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