How to create a Windows Forms program that can also be run on the command line

I created a program that has two projects: the Windows user interface and the "engine". I would like to make it possible for the user to execute the program from the command line by passing some arguments, and the mechanism will automatically execute them and spit out the results. OR the user can launch the user interface and select all of their options using the drop-down lists, etc., and then click the button to tell the engine to work. It actually already works for me, using something that I found a long time ago, but I'm trying to figure out if there is an easier way. The found method includes adding the Win32 class and creating external components for AllocConsole, FreeConsole and AttachConsole from kernel32.dll. Is there an easier way? Thanks.

+4
source share
3 answers

it's simple, make an application for Windows forms and in the Main method check the command line parameters and if your parameters are there, instead of calling

 Application.Run(new Form1()); 

just call your engine and start processing.

it is important to correctly encode your engine and also have a user interface using the same engine when the user runs these commands from the user interface, therefore, to avoid unnecessary code duplication.

we use this approach in many programs, and we are satisfied if you do not call Application.Run(...) , your program will simply terminate when the Main method ends.

+3
source

It is even easier. Create a console application. It will analyze the command line to get the parameters, and then it will call your engine.

Let the WinForms application worry about the GUI, let the console application worry about the command line.

+1
source

Create four separate configurations, two for the GUI and two for the console:

DebugConsole, DebugGui, ReleaseConsole, ReleaseGui

The above screenshot is for solution configurations. Youll have to do the same again for the project (in the screenshot above, a project that has these four configurations is Rummage). Here is an excerpt from Rummage.csproj :

 [...] <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugGui|AnyCPU' "> <OutputType>WinExe</OutputType> <DefineConstants>DEBUG;TRACE</DefineConstants> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseGui|AnyCPU' "> <OutputType>WinExe</OutputType> <Optimize>true</Optimize> <DefineConstants>TRACE</DefineConstants> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugConsole|AnyCPU'"> <OutputType>Exe</OutputType> <DefineConstants>DEBUG;TRACE;CONSOLE</DefineConstants> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'ReleaseConsole|AnyCPU'"> <OutputType>Exe</OutputType> <DefineConstants>TRACE;CONSOLE</DefineConstants> <Optimize>true</Optimize> </PropertyGroup> [...] 

Please note especially:

  • Console configurations have <OutputType>Exe</OutputType> (which makes them a console application), while Release configurations have <OutputType>WinExe</OutputType> .

  • Both console configurations have a CONSOLE constant. That way you can use #if CONSOLE [...] #else [...] #endif to get console / GUI specific code. For example, the Main method might look like this:

     [STAThread] static int Main(string[] args) { #if CONSOLE return MainConsole(args); #else return MainGui(args); #endif } 

    ... and then you can use WinForms stuff in MainGui and parsing the line at the MainConsole command prompt.

Thus, you can run (and debug) either the graphical interface or the console version in Visual Studio, and in the script assembly you can simply collect all of them (and in different directories or different EXE file names).

+1
source

All Articles