tags:

views:

1008

answers:

4

I want to run a console application that will output a file.

I user the following code:

Process barProcess = Process.Start("bar.exe", @"C:\foo.txt");

When this runs the console window appears. I want to hide the console window so it is not seen by the user.

Is this possible? Is using Process.Start the best way to start another console application?

A: 

We have done this in the past by executing our processes using the Command Line programatically.

Victor
+3  A: 

Check into ProcessStartInfo and set the WindowStyle = ProcessWindowStyle.Hidden and the CreateNoWindow = true.

Jeremy Reagan
For console apps, I've found that you just need the **WindowStyle = ProcessWindowStyle.Hidden**. You don't need the **CreateNoWindow = true**.
dangph
+11  A: 
        Process p = new Process();
        StreamReader sr;
        StreamReader se;
        StreamWriter sw;

        ProcessStartInfo psi = new ProcessStartInfo(@"bar.exe");
        psi.UseShellExecute = false;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;
        psi.RedirectStandardInput = true;
        psi.CreateNoWindow = true;
        p.StartInfo = psi;
        p.Start();

This will start a child process without displaying the console window, and will allow the capturing of the StandardOutput, etc.

Jason Miesionczek
Your answer is way more informative then mine +1
Jeremy Reagan
i just had to do the very same thing not 2 hours ago :)
Jason Miesionczek
You forgot to do anything with the StreamReaders and StreamWriter.
dangph
@dangph, those aren't nexessary for the process to execute, only if you need to interact with the process, or capture its console output. Since those requirements weren't specified in the question, i omitted them here.
Jason Miesionczek
+1  A: 

If you would like to retrieve the output of the process while it is executing, you can do the following (example uses the 'ping' command):

var info = new ProcessStartInfo("ping", "stackoverflow.com") {
    UseShellExecute = false, 
    RedirectStandardOutput = true, 
    CreateNoWindow = true 
};
var cmd = new Process() { StartInfo = info };
cmd.Start();
var so = cmd.StandardOutput;
while(!so.EndOfStream) {
    var c = ((char)so.Read()); // or so.ReadLine(), etc
    Console.Write(c); // or whatever you want
}
...
cmd.Dispose(); // Don't forget, or else wrap in a using statement
Pat