14

can anyone tell me how to spawn another console application from a Winforms app, but (A) not show the console window on the screen, and (B) still obtain the standard output of the application? Currently I have something like the following:

  Process SomeProgram = new Process();
  SomeProgram.StartInfo.FileName = @"c:\foo.exe";
  SomeProgram.StartInfo.Arguments = "bar";
  SomeProgram.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  SomeProgram.StartInfo.UseShellExecute = false;
  SomeProgram.StartInfo.RedirectStandardOutput = true;
  SomeProgram.Start();
  SomeProgram.WaitForExit();
  string SomeProgramOutput = SomeProgram.StandardOutput.ReadToEnd();

If I set RedirectStandardOutput to false, then the console app is hidden as expected, but I cannot get the standard output text. However, as soon as I set the RedirectStandardOutput to true, the window stops being hidden, although I am able to get the program's output.

So, I know how to make the console app run hidden, and I know how to get the program's output, but how do I get it to do both?

Many TIA

JamesPD
  • 243
  • 1
  • 3
  • 7
  • What is the problem with the current solution? – codymanix Dec 13 '10 at 15:44
  • The solution above does let me get the output of the console app, but the window is not hidden. If I remove the code for obtaining the standard output, the window *IS* hidden. I want the window hidden *AND* to get the standard output. – JamesPD Dec 13 '10 at 15:47

2 Answers2

34

You are missing the CreateNoWindow property which has to be set to true in your case.

Stefan Schultze
  • 9,240
  • 6
  • 35
  • 42
  • 4
    @JamesPD if Stefan's answer was the answer that solved your problem, you can reward him and mark it as such by accepting his answer - click the hollow tick to the left of his answer. – Mark Pim Dec 13 '10 at 15:58
2

I think it will help you:

System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = @"C:\Users\Vitor\ConsoleApplication1.exe";
pProcess.StartInfo.Arguments = "olaa"; //argument
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true; //not diplay a windows
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd(); //The output result
pProcess.WaitForExit();
Vítor Oliveira
  • 1,851
  • 18
  • 23