Hide Console Window in C# Console Application

Hi, How can I hide the console window in my Console Application project

Answer this question

Hide Console Window in C# Console Application

  • Ke Sun

    I have written and tested this code in VS.NET 2005 Pro , WindowsXP SP2.

    It appears that you may have a .net framework or VisualStudio install corruption.

    Check that you can add a reference to this dll manually (does the .tlb file creates properly in /bin/debug ); then try to clean then rebuild solution.

    Have you somewhat patched your user32.dll library or are you using a special software that may have patched it (for XP themes, etc); I'm just guessing there... cos if so, maybe that the C# compiler can't link with this patched version .

    The code is right, we've deployed our app that uses it throughout our LAN...

    If it's still not working, check Google for this error code and message...
    Good luck
    Mat


  • drazvan

    set the ProcessInfo Object 's property name CreateNoWindow to true;

    ProcessInfo prcInfo = new ProcesInfo("cmd.exe","/c dir");

    prcInfo.CreateNoWindow = true;



  • FoxyNet

    You can Pinvoke a call to FindWindow() to get a handle to your window and then call call ShowWindow() to hide the window.

    Before pasting some code though I’ve got to ask though... if you do not want your window visible, have you considered building your app into a Windows Service that will always be hidden from view



  • Patrick Sears

    I m writing a Windows Console application in C#. Where should I put this code main class or somewhere else
  • AirpowerDavid

    I know windows service writing. but I want to hide Console window (black window).
  • GusaMusa

    Hi,

    When we run a external process using Process Class console window(black window) displayed.

    I don want to display that black window than wat shd i do for that


  • Corrado Cavalli

    Hello,

    Thanks in advance to anyone that can help.

    The mini-project i'm doing (while the development server is down) is to build a Command Prompt (basically) with some text formatting abilities. I might go wild and try to throw in some more groovy windows on the application and maybe even an autocomplete. This is all for a buddy at work.

    I have managed to hide the black console window and had the output stream fill a label on a Windows application. As I run the process ("cmd", "/c dir") the label fills up nicely, but I can't seem to find a way to continue running commands as you would normally through the prompt while keeping the same process.

    Yes I am relatively new to this, apologies if I have missed something obvious.

    Timmy



  • JLarkin

    I get this compiler response:

    Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
    for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
    Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

    fatal error CS0009: Metadata file 'c:\WINDOWS\system32\user32.dll' could not be
    opened -- 'An attempt was made to load a program with an incorrect
    format. '

    what is this incorrect format it speaks of


  • enric vives

    Don't put ReadLine();

    Hope this helps,



  • Acco1953

    Check out ProcessStartInfo.CreateNoWindow


  • CMValdivia

    What worked for me was -

    Create a Windows Application in Visual Studio.

    Inside of the Program.cs instead of running a windows application/Form - instead just instantiate your class there.

    Normally after creating a Windows Application in VS your main method in the Program.cs file will look something like this:

    Application.EnableVisualStyles();

    Application.SetCompatibleTextRenderingDefault(false);

    Application.Run(new Form1());

    I comment out that and in its place I put my class that I want to instantiate and run - like this:

    //Application.EnableVisualStyles();

    //Application.SetCompatibleTextRenderingDefault(false);

    //Application.Run(new Form1());

    Class1 myClass = new Class1();

    myClass.RunThisPuppy();

    Hope this helps!


  • juicyjuice

    Try this:

    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);

    ...

    //Sometimes System.Windows.Forms.Application.ExecutablePath works for the caption depending on the system you are running under.

    IntPtr hWnd = FindWindow(null, "Your console windows caption"); //put your console window caption here

    if(hWnd != IntPtr.Zero)

    {

    //Hide the window

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

    }

    if(hWnd != IntPtr.Zero)

    {

    //Show window again

    ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA

    }



  • Rob Epler

    Well I managed to hide the console in VS2005 C# Express by changing the output type under Application in the project properties to Windows Application.
  • NewbieDude

    This is exactly right, Thanks Brandon for this usefull post.Below is the exact C# code for showing/hiding the method in a Console Application.

    using System.Runtime.InteropServices;

    namespace MyConsoleApp {
    class Program {

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);

    [DllImport("user32.dll")]

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

    [STAThread()]
    static void Main(string[] args)
    {
    Console.Title = "MyConsoleApp";

    if (args.StartWith("-w"))
    {
    // hide the console window
    setConsoleWindowVisibility(false, Console.Title);
    // open your form
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run( new frmMain() );
    }
    // else don't do anything as the console window opens by default
    }

    public static void setConsoleWindowVisibility(bool visible, string title)
    {
    // below is Brandon's code
    //Sometimes System.Windows.Forms.Application.ExecutablePath works for the caption depending on the system you are running under.
    IntPtr hWnd = FindWindow(null, title);

    if (hWnd != IntPtr.Zero)
    {
    if (!visible)
    //Hide the window
    ShowWindow(hWnd, 0); // 0 = SW_HIDE
    else
    //Show window again
    ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA
    }
    }
    }
    }

  • Hide Console Window in C# Console Application