Coder Perfect

A C# console application’s console window can be shown or hidden.


I looked up how to conceal one’s own console window on the internet. Surprisingly, the only options I could discover were using FindWindow() to locate the console window by its title. I dug a bit deeper into the Windows API and found that there is a much better and easier way, so I wanted to post it here for others to find.

How do you hide (and show) the console window associated with my own C# console application?

Asked by Timwi

Solution #1

Simply change the Output type from Console Application to Windows Application in the application’s Properties.

Answered by Fahad

Solution #2

Here’s how:

using System.Runtime.InteropServices;
static extern IntPtr GetConsoleWindow();

static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

const int SW_HIDE = 0;
const int SW_SHOW = 5;
var handle = GetConsoleWindow();

// Hide
ShowWindow(handle, SW_HIDE);

// Show
ShowWindow(handle, SW_SHOW);

Answered by Timwi

Solution #3

You could do the reversed and set the Application output type to: Windows Application. Then add this code to the beginning of the application.

[DllImport("kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr GetStdHandle(int nStdHandle);

[DllImport("kernel32.dll", EntryPoint = "AllocConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int AllocConsole();

private const int STD_OUTPUT_HANDLE = -11;
private const int MY_CODE_PAGE = 437;
private static bool showConsole = true; //Or false if you don't want to see the console

static void Main(string[] args)
    if (showConsole)
        IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
        Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = new Microsoft.Win32.SafeHandles.SafeFileHandle(stdHandle, true);
        FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
        System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(MY_CODE_PAGE);
        StreamWriter standardOutput = new StreamWriter(fileStream, encoding);
        standardOutput.AutoFlush = true;

    //Your application code

If showConsole is true, this code will display the Console.

Answered by Maiko Kingma

Solution #4

If you wish to conceal the console, why do you need a console application? =)

Instead of Console program, I recommend setting the Project Output type to Windows Application. It will not display a console window, but will perform all of the functions that the Console application does.

Answered by Sasha

Solution #5

Here is a link to my post:

In a Windows application, show the console

You can create a Windows application (with or without a window) and display the console in whatever way you like. The console window does not appear until you expressly reveal it when using this approach. It’s what I use for dual-mode apps that I want to execute in console or GUI mode depending on how they’re launched.

Answered by Anthony

Post is based on