1

I am trying to add command-line support for a desktop application I built so that I can run its commands from elsewhere without display the UI. This is approximately what my Main method looks like:

[STAThread]
private static void Main(string[] args)
{
    // Run the desktop application
    if (args.Length == 0)
    {
        AppDomain.CurrentDomain.ProcessExit += OnProcessExit;

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        var mainForm = new Form1();
        Application.Run(mainForm);
        mainForm.BringToFront();
    }
    // Restart the session and log in console
    else if(args[0] == "restartsession")
    {
        Console.WriteLine("Restarting session...");
    }
    // argument not recognised (log to console)
    else
    {
        Console.WriteLine($"Argument \"{args[0]}\" not recognised");
    }
}

This appears to work just fine, apart from the fact that the Console window is not displayed when calling Console.WriteLine().

This is due to my project's output type being set to Windows Application (I set this in properties). If I change it to Console Application the console window appears with my logging, but then it also displays when opening the "UI" desktop version (ie running the app without any arguments).

Is there a way to change the output type depending on the arguments passed into Main()? For example something like this perhaps:

private static void Main(string[] args)
{
    if (args.Length == 0)
    {
        Application.OutputType = WindowsApplication
    }
    else if(args[0] == "restartsession")
    {
        Application.OutputType = ConsoleApplication
    }
}

I was able to find this link but it appears to be old and not relevant any more. Is there some other way of achieving this?

Bassie
  • 9,529
  • 8
  • 68
  • 159
  • 1
    no, you will have to build 2 exe's with either type. – leppie Jun 30 '16 at 13:44
  • @leppie Thanks for your comment leppie. Is this standard practice for creating exes with commandline support? I ask because we would prefer 2 have just 1 application due to space constraints. – Bassie Jun 30 '16 at 13:44
  • 1
    You only need to 2 small (4KB at most) exe's and a big dll. – leppie Jun 30 '16 at 13:46
  • 1
    In Windows, the PE format (used for executables) only allows one subsystem type. In this case, that is *either* a Windows application or a Console application. You have to pick one, you cannot have both. However, [a Windows application can allocate and display a console window](http://stackoverflow.com/questions/472282/show-console-in-windows-application) on-demand. Or you can just create your own window with a textbox/richtextbox/listview/etc. and treat it like a console. – Cody Gray - on strike Jun 30 '16 at 13:47

2 Answers2

0

As Leppie suggested in a comment, this is not possible and I will have to build 2 separate applications - one for the desktop and one for the command-line.

A possible solution for this would be to compartmentalise the RestartSession functions into a dll and then have 2 small exes calling the same dll (one for the desktop and one for the console)

Bassie
  • 9,529
  • 8
  • 68
  • 159
0

Leppie has the right answer in the comments. For scenarios like this, your actual application project should be very small. It should include only things that are specific to that application type.

All of your business logic, application shared initialization logic, etc. should be moved to a different project which is consumed by both.

Tim Copenhaver
  • 3,282
  • 13
  • 18