C#:外部运行控制台程序为隐藏

C#:外部运行控制台程序为隐藏

问题描述:

谁能告诉我如何从产卵一个WinForms应用另一个控制台应用程序,但是(A)不显示在屏幕上的控制台窗口,和(B)仍获得应用程序的标准输出?目前,我有类似如下:

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

如果我设置RedirectStandardOutput为false,那么的控制台应用程序如预期隐藏,但我不能让标准输出的文本。但是,当我设置RedirectStandardOutput为true,则窗口不再是隐藏的,虽然我能够获得程序的输出。

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?

许多TIA

您缺少的 CreateNoWindow 里面有你的情况设置为真正属性。

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