3

Sorry my title is rather vague but I don't know how to concisely label what I want to do.

I have a winform application which were trying to dual-purpose so it can also be run as a command line utility by our automation system. In the programs Main() I am conditionally executed the desired code. The only problem is that (despite logging to Console) a command window is not being launched. What do I need to do to make the application launch a command window and direct stdout to that?

Below is my main (HeadlessExecution is correctly being executed):

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        if (!ValidateCommandLineArgs())
            Application.Run(new TestResultForm());
        else
        {
            HeadlessExecution();
        }
    }
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • 2
    possible duplicate of [Show Console in Windows Application?](http://stackoverflow.com/questions/472282/show-console-in-windows-application) – Ani Jul 09 '12 at 20:42
  • Unfortunately I understand exactly what you meant, but have only a vague idea of how to do it. :S – Wug Jul 09 '12 at 20:43
  • @ananthonline Yeah I would say this is a duplicate however Oded posted a useful solution here so perhaps it should be left open. – evanmcdonnal Jul 09 '12 at 20:56

1 Answers1

4

Extract all the logic into a different, class assembly.

Reference this assembly in a winforms and in a console application. Voila.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    @Wug - I suggest you read the answers and links for the [question](http://stackoverflow.com/questions/472282/show-console-in-windows-application) that ananthonline posted in a [comment](http://stackoverflow.com/questions/11402753/dual-purposing-an-application-to-run-as-winform-or-command-line-utillity/11402782#comment15034999_11402753). There are _only_ workarounds. As such, this is a less than crazy option. – Oded Jul 09 '12 at 20:44
  • So many other programs do exactly this though. I highly doubt there is no way to do it in C#. I have written programs that acted this way in C++ (for windows) and a huge number of programs on linux and unix do it too (emacs? gedit? baobab? etc...). There HAS to be a way to do it. – Wug Jul 09 '12 at 20:52
  • though maybe the winform bit is throwing me for a loop. – Wug Jul 09 '12 at 20:52
  • Agreed, this is the "correct" design approach - but I posted the other answer in case such a "redesign" was not possible. – Ani Jul 09 '12 at 20:52
  • @ananthonline - Those links look interesting, and rather scary at the same time ;) – Oded Jul 09 '12 at 20:53
  • @Wug I was hoping there was a more elegant solution but it doesn't seem like there is. A lot of winforms application behave in this way as well (a lot of Visual Studio components have command line equivalents). – evanmcdonnal Jul 09 '12 at 20:54
  • Well, I know with Python you have a `python.exe` and a `pythonw.exe` (the latter not opening the console). GVim portable has `vim.exe` and `gvim.exe`. So it may be that this approach is "the correct way". – Wayne Werner Jul 09 '12 at 21:16