1

I am programming an application using the command line application output type to display debug information in the console.

I would like to hide the console when compiling the application for release. Not showing the console can easily be done by going into the project properties, the application tab and change the output type to windows application.

But anyway I would like to give the user opportunity to open the application with console even in Release. For this I added Dev button and when user click it I would like that an application open console with all debug log output.

I found such answer on SO

https://stackoverflow.com/a/7828186/5709159

And it is works, when the application in dev console also open if in Release console not open.

So, I see two possible ways

  1. When user in Release mode he can click on Dev button and console will be opened with all previous log.
  2. Add some variable (like DevMode) to Properties file, like in link above. When user in Release mode he can click on Dev button I save DevMode = true(in file) and user need to reload the app and open an app again, but now I can see that DevMode == true and according to this open the application with console even in Release mode

Question is - is it possible to do it ?

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121

2 Answers2

0

The Console is a very old thing, with limitations going back all the way to the early DOS times. When the idea of Windows and Graphical User Interfaces had not yet been properly through up. Unless you are comfortable handling Native Windows API calls, this is propably not something you can change.

That being said:

  • You can dispaly a console in paralell to a GUI Applications normal Windows. It does use the Native API call, but seems comparatively easy as far as those go. It might also work to have a Console Appear from a no Display Application.
  • Maybe it would be enough to imitate the console Window? If your programm starts a console programm, it can hide the console window and redirect the Input and both Output Streams. You could then create a Forms Window that imitates a normal console and allows the user to talk via those streams to the invisible console.
Christopher
  • 9,634
  • 2
  • 17
  • 31
  • I did not understand something, but in first case I am successfully open console, but this console does not show any `Console.WriteLine()` log output... And with your second solution I did not get, what do you mean( could you explain please? Or give a example? Thanks – Sirop4ik Feb 03 '20 at 16:40
  • @AlekseyTimoshchenko That the (new) console might not contain the previous output I suspected. You would propably need to store that. | With Option 2, what part did you not get? That console Programms have Input and Output Streams? That you can redirect them? That you can show to contents to and get input from the user in a mock console window? – Christopher Feb 03 '20 at 16:43
0

You could compile your programm as console application (even in case it is a Windows Forms application) and use the following code to programmatically hide the console window:

void HideConsole()
{
    MyConsole.HideConsole();
}

internal class MyConsole
{
    [DllImport("kernel32.dll")]
    static extern IntPtr GetConsoleWindow();

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    [DllImport("kernel32")]
    static extern bool AllocConsole();

    const int SW_HIDE = 0;
    const int SW_SHOW = 5;

    public static void HideConsole()
    {
        var handle = GetConsoleWindow();

        // Hide
        ShowWindow(handle, SW_HIDE);
    }

    public static void ShowConsole()
    {
        AllocConsole();
    }
}

There is also the other way around, compile your program as Windows Forms application and programmatically show the console window:

void ShowConsole()
{
    MyConsole.ShowConsole();
}

But note, this piece of code may not work directly within Visual Studio as Visual Studio could redirect the console input. However, if you directly run your application it should work fine.

InputOutput
  • 61
  • 1
  • 5
  • mm, it looks really nice solution. But I now in every place where `Console.WriteLine()` I get `System.IO.IOException: 'The handle is invalid.` Is it mean that now I have to wrap every place with `Console.WriteLine()` with `try/catch`? – Sirop4ik Feb 03 '20 at 16:35
  • Check my updated code which uses a different approach now. – InputOutput Feb 03 '20 at 16:56