Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

process - C#: Run external console program as hidden

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...