C# – Make console application invisible

invisible-iconSometimes it can be handy to hide your console application from the users, to avoid complications in the example. Here is how to do it.

First, we need to import a few dlls and a few functions from those dlls. We will also make a few variables for the show and hide parameter in ShowWindow.

In the class before the main function.

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImport("Kernel32")]
private static extern IntPtr GetConsoleWindow();

const int SW_HIDE = 0;
const int SW_SHOW = 5;

Then we will run the function to get hold of the handle for the console window, and run the hide function (ShowWindow, with hide parameter).

// Hide the window
IntPtr hwnd;
hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_HIDE);

To “unhide” the window, if you need to do that before the application ends.

// Show the window
IntPtr hwnd;
hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_SHOW);

That’s it really.

Happy hiding!

About Author

Related Posts

C# Reference Types

Understanding C# Reference Types

One of the key features of C# is its support for C# reference types, which allow developers to create complex, object-oriented applications. In this blog post, we…

c# value types

Understanding C# Value Types

C# is a powerful programming language that is widely used for developing a wide range of applications. One of the key features of C# is its support…

C# check if server is online

C# check if server is online directly from your code. Check servers or services like web servers, database servers like MySQL and MongoDB. You can probably check…

C# Convert Int to Char

C# convert int to char google search is giving some result which I think is not directly what some people want. Most results are giving instructions on…

c# bash script

C# Bash Script Made Easy

There are many reasons why it could be handy to run a bash script from a C# application. In 2014, before I changed to a Mac as…

go hello world

Golang Hello World, Get a Easy Fantastic Start

Golang hello world example tutorial. I assume you are new to the golang language since you found this website. Go is one of the latest programming languages…

Leave a Reply