views:

7005

answers:

7

Is there a way to hide the console window when executing a console application?

I am currently using a Windows Forms application to start a console process but I don't want the console window to be displayed while the task is running.

+18  A: 

if you are using the ProcessStartInfo class you can set the window style to hidden.

System.Diagnostics.ProcessStartInfo start =
      new System.Diagnostics.ProcessStartInfo();     
start.FileName = dir + @"\Myprocesstostart.exe";
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
Adam Markowitz
Just what I was going to post :)
Jon Skeet
Holy crap - someone faster than Jon! =)
Erik Forbes
I had to throttle his internet connection to beat him ;)
Adam Markowitz
A: 

I know I'm not answering exactly what you want, but I am wondering if you're asking the right question.

Why don't you use either:

  1. windows service
  2. create a new thread and run your process on that

Those sound like better options if all you want is to run a process.

Nathan Koop
There are plenty of scenarios where launching a utility console application is perfectly legitimate.
Adam Robinson
In my case a separate process is necessary because of the client/server model that the software I am working on uses.
Aaron Thomas
+4  A: 

You can use the FreeConsole API to detach the console from the process :

    [DllImport("kernel32.dll")]
    static extern bool FreeConsole();

(of course this is applicable only if you have access to the console application's source code)

Thomas Levesque
I wanted to not show the console. While `FreeConsole` does what its name says, it doesn't prevent Windows from showing the console before it is called.
Jader Dias
Then compile the project as a Windows Application, not a Console Application
Thomas Levesque
it worked, thanks
Jader Dias
+2  A: 

If you're creating a program that doesn't require user input you could always just create it as a service. A service won't show any kind of UI.

Chris Bregg
+2  A: 

If you wrote the console application you can make it hidden by default.

Create a new console app then then change the "Output Type" type to "Windows Application" (done in the project properties)

Simon
+2  A: 

if you are using Process Class then you can write

yourprocess.StartInfo.UseShellExecute = false;
yourprocess.StartInfo.CreateNoWindow = true;

before yourprocess.start(); and process will be hidden

+1  A: 

Simple answer is that: Go to your console app's properties(project's properties).In the "Application" tab, just change the "Output type" to "Windows Application". That's all.

Cihan