Implementation of “hybrid” (console or GUI) .NET applicatios

December 29, 2014, (updated on October 19, 2015), 1 comment, Software Development

Have you ever had the need to use an application (.exe) as a command line tool when started in the console (cmd.exe) and as GUI application when started from the Explorer shell? This article explains a possible implementation for this requirement.

To be clear: I didn’t find the perfect solution; a Windows .NET application is either a console application or a Windows application. Either type can be used in the opposite mode, but both variants have some drawbacks.

This article describes the solution which I think has the least disadvantages. The solution works by defining the application as console application and show the WPF window if the application is not executed in a console. The only drawback I’ve found is that the console window is shown for some milliseconds at application start.

The other approach is to define the application as Windows application, but then you will have a lot of problems when using the application in console mode, e.g. the application is started in a second window, the output is not printed to the original console and the console does not wait until the application has executed because Windows forked out the process. This is why I prefer the first solution.

Implementation

The following steps describe how to implement such a “hybrid” application:

  1. Create new WPF application project
  2. Add a Program.cs file with the following content:

    public static class Program
    {
        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        static extern bool FreeConsole();
    
        [STAThread]
        public static int Main(string[] args)
        {
            if (args != null && args.Length > 0)
            {
                // TODO: Add your code to run in command line mode
                Console.WriteLine("Hello world. ");
                Console.ReadLine();
                return 0;
            }
            else
            {
                FreeConsole();
                var app = new App();
                return app.Run();
            }
        }
    }
    
  3. Add a constructor in App.xaml.cs and call InitializeComponent():

    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
        }
    }
    
  4. Open project settings

    1. Select “Program.Main” as application entry point
    2. Change the type of the project to “Console application”

Final thoughts

Even if there are some solutions, I think it is better to provide two different applications which call shared assemblies. This way you avoid all the mentioned issues and do not have to live with the drawbacks of the available workarounds.

Further reading

For more information on this topic, head to the following sources:

Tweet about this on TwitterShare on FacebookEmail this to someoneShare on TumblrShare on LinkedIn

Tags: , , , ,

One response to “Implementation of “hybrid” (console or GUI) .NET applicatios”

  1. David Cater says:

    Nice job! Using this today. This was exactly what I needed. Now I want to see if I can use the Xaml parsing engine when running as a console application to instantiate a DocumentTree consisting solely of non-UI objects (basically just leveraging the Xaml architecture for object instantiation).

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax