C#:以隐藏方式运行外部控制台程序

发布于 2024-10-07 02:00:59 字数 752 浏览 2 评论 0原文

谁能告诉我如何从 Winforms 应用程序生成另一个控制台应用程序,但 (A) 不在屏幕上显示控制台窗口,并且 (B) 仍然获得应用程序的标准输出?目前我有类似以下内容:

  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,窗口就会停止隐藏,尽管我能够获取程序的输出。

所以,我知道如何使控制台应用程序隐藏运行,并且我知道如何获取程序的输出,但如何让它同时执行这两项操作?

许多TIA

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转角预定愛 2024-10-14 02:00:59

您缺少 CreateNoWindow 属性,在您的情况下必须将其设置为 true

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

情徒 2024-10-14 02:00:59

我认为这会对你有所帮助:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文