How to hide the Console Application in C#

How to hide the console application in C#

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Console Application. Name your project "HideConsole" and then click OK

hide console application in c#Step 2: Add code to Program class as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace HideConsole
{
    class Program
    {
        [DllImport("Kernel32.dll")]
        private static extern IntPtr GetConsoleWindow();
        [DllImport("User32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int cmdShow);

        static void Main(string[] args)
        {
            Console.WriteLine("Press any key to hide me.");
            Console.ReadKey();
            IntPtr hWnd = GetConsoleWindow();
            if (hWnd != IntPtr.Zero)
            {
                ShowWindow(hWnd, 0);//hide
                Thread.Sleep(5000);//5s
                ShowWindow(hWnd, 1);//show
            }
            Console.ReadKey();
        }
    }
}

VIDEO TUTORIALS

 

Related