Home > dotnet > Hide Console Window in Console Application In C#.Net

Hide Console Window in Console Application In C#.Net

1) Create the Console application in C# dotnet

2) Now in the static void Main(string[] args) function put the below line

//give the title of running application

Console.Title = “TestHideApplication”;

//put your console window caption here

IntPtr hWnd = FindWindow(null, ” TestHideApplication “);

if (hWnd != IntPtr.Zero)

{

//Hide the window

ShowWindow(hWnd, 0); // 0 = SW_HIDE

}

3) in the global declation put the below code

//add namespace using System.Runtime.InteropServices;

[DllImport("user32.dll")]

public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]

static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

Thanks

  1. MakiS
    September 7, 2008 at 12:58 pm | #1

    hmmm.. not work at me… if u are working at C# please add me at msn : kataras2008@hotmail.com or send me e-mail at : kataras2006@hotmail.com ..

    i am 15 years old and i am learning C#…

  2. September 8, 2008 at 3:08 am | #2

    Hi Makis,
    good work Makis.
    can you describe in detail which application you use and how to apply above code in your application..

    thnx

  3. dsky
    January 28, 2009 at 3:52 am | #3

    This solution find a console window with given title and hides it. The problem here is that it will hide all the windows matching this title. I believe a better solution would be to call a kernel32 API FreeConsole(). It will detach the console attached to the application and close it if not in use.

  4. faid
    September 18, 2009 at 1:00 am | #4

    thank u soooooooooooooooo much

  5. habeeballah
    December 12, 2009 at 8:12 am | #5

    No need of any code as such. In order to hide any Console app, just open the Project Properties. Under the Application Page, change the “Output type” from Console Application to Windows Application.

    Thats it. Your Console app will be forever hidden.

    • Rahul
      January 11, 2010 at 8:22 am | #6

      What if I am calling some other application not made in c# (or say .NET)?

    • Jess Pilor
      May 14, 2010 at 2:08 am | #8

      Yep this is the best way to do it. No code needed :D

    • KKK
      April 7, 2011 at 6:47 am | #9

      This information is indeed usefull when you are using atistream on win>XP.
      The driver has the very annoying feature: it requires an attached console in order to not to crash on building opencl code.

    • donatogreco
      April 30, 2011 at 10:51 pm | #10

      THIS is the solution I was looking for my C#.Net Console app to Hide.

      Thanks a lot

  6. AEM
    February 15, 2011 at 6:25 am | #11

    It worked. Good work :-)

  7. Giga Baitashvili
    March 3, 2011 at 1:13 pm | #12

    Nice one.
    But I would not use FindWindow:
    There is a function for that: GetConsoleWindow() in kernel32.dll.
    Retrieves handle of console window owned by current process.

    [DllImport("kernel32")]
    static extern IntPtr GetConsoleWindow();

    returns IntPtr.Zero if current process does not have a console window.

  1. January 27, 2009 at 8:52 am | #1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 244 other followers