How do I need to change my WinForms application to run in the console?

I have a .NET WinForms application written in C #. To support batch operations, I would like the application to run in the console.

Is it possible to have an application that detects at startup whether it is running on the console or not?

What changes need to be made to achieve this behavior?

+3
source share
3 answers

Your solution should have a Program.cs file, this file contains:

static void Main() 
{
}

You will notice that in this method there is something like:

Application.Run(new Form1());

Your form is actually running here, so you can do your own Main()as follows:

static void Main(string[] args)
{
   if(args.Length < 1)
   {
      Application.Run(new Form1());
      return;
   }
   else
   {
     // Handle your command line arguments and do work
   }
}

, , . , , .

+8

WinForms, AllocConsole. , # pinvoke .

, , , .

+2

You can use the main method in program.cs and determine if command line parameters are passed if they perform batch processing if you do not show a graphical interface.

public static void Main(string[] args)
{
 if (args.count > 0) {
  //batch
 } else {
  //gui
 }

}
+1
source

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


All Articles