Is it possible to run a Windows application in a console window?

I have a Windows application that I wrote in C # 4 and WPF; Now they asked me if I can add a command line parameter (for example, / console) that will make it run as a console application so that it can be launched by the task scheduler.

Is this possible with modern applications? Or do I need to create a separate console application?

UPDATE : can I just emphasize that this is a WPF application. There is no convenient entry point static void Main(string[] args) for connecting. But PM will still like the application to be able to run from the command line ...

FINAL UPDATE : The trick @ RodH257 points to is that the WPF application encodes the expected static void Main. You can add your own class using the method with the same name and in the properties of the project construction, setting it as the launch object for the executable file. You will also need the [STAThread] attribute in the method so that WPF also works correctly.

+4
source share
6 answers

You can either turn it into a console application or manually show the first WPF dialog. just as if you were creating a DLL and launching a WPF window as shown below

  static class Program { static void Main(params string[] args) { if (args.Length > 0) { //do stuff here return; } Window1 window = new Window1(); window.ShowDialog(); } } 

OR, look at this link to edit the entry point to the current WPF project and proceed based on the arguments. How to write your own main method for a WPF application?

EDIT: Updated post to make it more understandable for a specific situation.

+1
source

Yes, the Windows GUI application has Main as a console application. So the trick is to do Main preprocessing before starting the user interface loop. It should be noted that if you want to use the console from an application compiled as a .NET Windows application, in Visual Studio you will need AllocConsole , since by default you will not get it.

This section of the MSDN forum discusses a few more "hybrid console / graphics applications."

+4
source

You can start it from the console application using the dos command, but if you do not change the start procedure (in Main() ) when it starts, it will load and display the main window ... If you do not want the main window to be displayed, simply recode the main procedure so that the Main() parameter disables this option ...

Instead of this:

  [STAThread] static void Main() { Application.Run(new MainForm()); } 

code this:

  [STAThread] static void Main(params string[] args) { if(args.length <= 1) Application.Run(new MainForm()); else if (args[1].StartsWith("C")) // "C" for "C"onsole version // Do whatever needs to be done to start // functionality for console app version } 

Then, when you run it, add the / C switch:

  C:\MyCurrentDirectory>MyWinFormsApplicationName.exe /C 
+2
source

It extremely depends on how the program was originally written.

If you wrote your logic in a loosely coupled way, you can create another console application and reuse your logic. This is manual programming / refactoring.

0
source

If it is launched by the task scheduler, you do not need a screen at all. remove startupuri and manually launch windows depending on the communication line.

if it starts the console, just do whatever processing you need. Otherwise, launch your windows ...

0
source

Here's a sample article / code showing how this is done . The type of assembly is important. Your compiler only allows you to have one build target (e.g. GUI, command line).

-1
source

Source: https://habr.com/ru/post/1384824/


All Articles