I just made a silent application to check how much time my little brother stays on his computer. But, I needed to make an application that doesn’t show any window. Now, I show you my personal solutions to do this for the Console Application Projects and for the Windows Forms Application Projects.
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:
System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
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{ [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); static void Main(string[] args){ IntPtr h = Process.GetCurrentProcess().MainWindowHandle; ShowWindow(h, 0); while (true){ System.Threading.Thread.Sleep(1); //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#”