Show/Hide the console window of a C console application
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
In C#, you can show or hide the console window at runtime using the Windows API functions ShowWindow and GetConsoleWindow via P/Invoke. This is useful for background services, system tray applications, or programs that should run without a visible console. You can also set the project output type to "Windows Application" to hide the console by default at startup, then show it programmatically when needed.
Using P/Invoke to Show/Hide
GetConsoleWindow() returns the handle to the console window associated with the current process. ShowWindow() changes its visibility state.
Helper Class
Wrap the P/Invoke calls in a reusable class:
Hide Console at Startup (Project Setting)
Change the project output type to suppress the console window from the start:
Or in Visual Studio: Project Properties > Application > Output Type > Windows Application.
With WinExe, no console window appears at startup. You can still use Console.WriteLine() — the output goes nowhere unless you allocate a console:
System Tray Application with Hidden Console
A common pattern is to hide the console and show a system tray icon:
Redirecting Console Output
When the console is hidden, you may want to redirect output to a file:
.NET Generic Host (Modern Approach)
For background services in .NET 6+, use the Generic Host instead of hiding a console:
This runs as a proper service without any console window and can be deployed as a Windows Service with UseWindowsService().
Common Pitfalls
- Windows-only APIs:
GetConsoleWindow,ShowWindow,AllocConsole, andFreeConsoleare Windows-specific. They do not work on Linux or macOS. For cross-platform apps, use .NET Generic Host for background services instead. - GetConsoleWindow returning IntPtr.Zero: If the process has no console (e.g., started as a Windows Service or with
WinExeoutput type),GetConsoleWindow()returnsIntPtr.Zero. Check for this before callingShowWindow(). - Console.ReadLine blocking after hide: If the console is hidden while
Console.ReadLine()is waiting, the application hangs because the user cannot type. Cancel or skip input reads before hiding. - Multiple calls to AllocConsole:
AllocConsole()fails if a console is already attached. CallFreeConsole()first if you need to reallocate, or check the return value ofGetConsoleWindow(). - Flash of console window at startup: With
OutputType=Exe, the console briefly appears beforeShowWindow(handle, SW_HIDE)runs. UseOutputType=WinExeandAllocConsole()only when needed to avoid the flash.
Summary
- Use
GetConsoleWindow()andShowWindow()via P/Invoke to show/hide the console at runtime - Set
OutputTypetoWinExein the.csprojto prevent the console from appearing at startup - Use
AllocConsole()to create a console window on demand forWinExeapplications - Redirect
Console.SetOut()to a file when the console is hidden to preserve log output - For modern .NET background services, use
Host.CreateApplicationBuilderwithBackgroundServiceinstead of hiding a console window - These APIs are Windows-only — use cross-platform alternatives for Linux/macOS deployment
Related reading
- shuffle rearrange randomly a Liststring
- Shuffle string c
- SignInManager,what it is and how,when to use?
- Simple accord.net machine learning example
- Simple Delegate delegate vs. Multicast delegates
- Simple description of worker and I/O threads in .NET
- Simple description of worker and I/O threads in .NET
- Simplest way to do a fire and forget method in C?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.