Hide Console Window in C#


Console Application Projects

We must find the handle of the console window, and then hide it by using the ShowWindow API. Searching online, this is usually done with the FindWindow API that looks only the window title. But this method has some flaws. Instead, I used a .NET property:


Now, I want to show you a simple program that uses this property.

Create a new Console Application project and call it MyHideConsole. Now copy and past this code to the Program.cs file:

using System;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace MyHideConsole{
    class Program{
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        static void Main(string[] args){
            IntPtr h = Process.GetCurrentProcess().MainWindowHandle;
            ShowWindow(h, 0);

            while (true){
                //Do what you want

Windows Forms Application Projects

This time, it is quite simple. To have a silent application you just don’t show any window! The main form is called in the main function, you must just need to remove this line:

Application.Run(new MyForm());

11 responses to “Hide Console Window in C#

  1. Eastwood

    Peferct shot! Thanks for your post!

  2. gab

    how will you reshow the console again?

    1. miro.mannino

      just recall the ShowWindow with the value 5 (i.e. SW_SHOW) instead of 0 (i.e. SW_HIDE)

  3. Melika

    thank you so much! that simple program help me greatly! thanks!

  4. Sameh

    While(true) may enter your application to an endless loop

    1. miro.mannino

      Ehehe yes of course it does. It was just as an example! 🙂

  5. Alix Hamidou

    franchement tu est le king!!!
    tu viens de m’aider mais tu ne t imagine même pas

    1. Miro

      Merci beaucoup!

  6. Rich B.

    Rather than trusting “System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle”, for some reason, I think it may be more reliant to use this instead:

    in class “Program”:
    static extern IntPtr GetConsoleWindow();

    in method “Main”:
    IntPtr h = GetConsoleWindow();

    I could be wrong … but … using “GetConsoleWindow” seems more certain to return the handle for the console window … than hoping the console window is always the “main window”.

    1. Miro

      Yes this is a good hint! Because my main focus was just for console applications, but it’s true, if you also have some other windows definitely this is a good solution! Thanks!

  7. Leia Marshall

    Okay wow, I have looked at so many methods to do this. This worked perfectly for me. Thanks 😀

Leave a Reply to Leia Marshall Cancel reply
