Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF / Console Hybrid Application

Tags:

c#

wpf

I writing an application what can either be run on the command line, or with a WPF UI.

[STAThread]
static void Main(string[] args)
{
    // Does magic parse args and sets IsCommandLine to true if flag is present
    ParseArgs(args);     

    if(IsCommandLine)
    {
        // Write a bunch of things to the console
    }
    else
    {
        var app = new App();
        app.Run(new Window());
    }
}

I set the project's Output type to Console Application, I get a console window that popups if I try to execute it by double-clicking the exe. I don't want to show the console window to the user if the flag is not set (passed in via command args).

However, if I set the project's Output type to Windows Application, the double-click behaviour is fine, but when I run it in the console, I get no console output (Console.Writeline)

like image 580
Terenced Avatar asked Mar 17 '11 12:03

Terenced


1 Answers

Your best bet would be to abstract out the code that actually does the work to a separate class library that has no UI and then create two applications one Console, the other WPF that call this.

A console application and an WPF application have entirely different application models so you can't reuse the same code in both applications.

Having a separate class library allows you do other things like use it in other applications such as a web site or client/server architecture.

like image 82
ChrisF Avatar answered Oct 06 '22 07:10

ChrisF