Eric Anastas
Eric Anastas

Reputation: 22213

Is it a bad idea to combine a GUI program and a console application into a single EXE?

I'm writing a little utility which has a WPF based interface. I also want to be able to automate the same tasks performed by the utility by executing a program with command line parameters. Is it a bad idea to combine both of these tasks into one program? I have the actual logic and functionality that my tool performs in a separate shared library. So I wouldn't be duplicating a whole lot of code if they were separate.

I was thinking of doing something like this in my App.cs file

private void Application_Startup(object sender, StartupEventArgs e)
    {
        if (e.Args.Length > 1)
        {
            //Go do automated tasks
        }
        else
        {
            //open GUI

            Window window = new Window();

            this.MainWindow = window;

            window.Show();
        }
    }

Upvotes: 5

Views: 1282

Answers (3)

Hans Passant
Hans Passant

Reputation: 941217

You are not actually creating a console so this is not a console mode app.

A GUI program accepting arguments is entirely normal. The boilerplate example is a file association. Like double-clicking a .sln file in Windows Explorer starts Visual Studio which then loads the solution. Double-clicking a bitmap starts MS-Paint. Etcetera. The path of the clicked file is passed to the program through a command line argument, just as if you typed it in the command interpreter.

Whether or not you create a window is entirely up to you. Don't forget the need to report problems.

Upvotes: 2

WildCrustacean
WildCrustacean

Reputation: 5966

You can definitely do this to run automated tasks or set options from the command line, but keep in mind that a WPF application has no console window, so you won't be able to use Console.WriteLine() or anything like that. If you don't need console output, maybe that isn't a big deal.

There may be some awful win32 hack to attach a console window to a WPF application, but I wouldn't recommend that. If you need a real console, building both a WPF and console front end for your library may be the way to go.

I would also recommend Options.cs to do command line argument processing in C#.

EDIT: I may be wrong about all this, see the comments.

ALSO: Related information about windows application vs. console application: Difference between Windows and Console application

Upvotes: 0

gudatcomputers
gudatcomputers

Reputation: 2882

In this case I would create a functional class library that is invoked from different Host projects. MyApp.WPF references MyLib as a dll...MyApp.Console references MyLib as a dll.

edit I see that you already have the class library with the functionality segregated. What is the real benefit of keeping it in the same application then?

Upvotes: 1

Related Questions