Hide console Window in C#

To hide console window in a C# console application your Program.cs file should be similar to this:

namespace mynamespace
{
    class Program
    {
        [DllImport("kernel32.dll")]
        static extern IntPtr GetConsoleWindow();
 
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
 
        const int SW_HIDE = 0;
        const int SW_SHOW = 5;
 
        static void Main(string[] args)
        {
            var handle = GetConsoleWindow();
 
            // Hide
            ShowWindow(handle, SW_HIDE);
        }
    }
}
Categories: C# Tags: , , ,
%d bloggers like this: