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\")
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
We have done this in the past by executing our processes using the Command Line programatically.
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.
Check into ProcessStartInfo and set the WindowStyle = ProcessWindowStyle.Hidden and the CreateNoWindow = true.