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

Advertisements

13 comments

  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. 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. […] until finished. There are also ways to hide the Console window (check this link for example: Hide Console Window in Console Application In C#.Net « DotNet Friends) once the app is started, but a Console window will appear quickly anyway. There is also sort of a […]

  4. 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.

  5. thank u soooooooooooooooo much

  6. habeeballah · · Reply

    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.

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

      1. Then you must use the technique described here :-

        http://www.codeproject.com/KB/winsdk/runsilent.aspx
        ———————————————————-

    2. Jess Pilor · · Reply

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

    3. 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.

    4. donatogreco · · Reply

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

      Thanks a lot

  7. It worked. Good work 🙂

  8. Giga Baitashvili · · Reply

    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.

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 )

Google+ photo

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

Connecting to %s