C# – Make console application invisible


invisible-iconSometimes it can be handy to hide your console application from the users, to avoid complications in the example. Here is how to do it.

First, we need to import a few dlls and a few functions from those dlls. We will also make a few variables for the show and hide parameter in ShowWindow.

In the class before the main function.

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

[DllImport("Kernel32")]
private static extern IntPtr GetConsoleWindow();

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

Then we will run the function to get hold of the handle for the console window, and run the hide function (ShowWindow, with hide parameter).

// Hide the window
IntPtr hwnd;
hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_HIDE);

To “unhide” the window, if you need to do that before the application ends.

// Show the window
IntPtr hwnd;
hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_SHOW);

That’s it really.

Happy hiding!

Have any Question or Comment?

Leave a Reply