【发布时间】:2013-01-10 18:47:37
【问题描述】:
我看到了几个关于如何启动进程并将数据推送到标准输入的问题,但没有看到如何控制它们的输出去向。
首先是我当前的代码,从控制台模式 C# 应用程序运行:
// Prepare the process to run
ProcessStartInfo start = new ProcessStartInfo();
// Enter in the command line arguments, everything you would enter after the executable name itself
start.Arguments = " -";
// Enter the executable to run, including the complete path
start.FileName = "doxygen.exe";
// Do you want to show a console window?
start.WindowStyle = ProcessWindowStyle.Normal;
start.CreateNoWindow = false;
start.RedirectStandardInput = true;
start.UseShellExecute = false;
// Run the external process & wait for it to finish
using (Process proc = Process.Start(start))
{
//doxygenProperties is just a dictionary
foreach (string key in doxygenProperties.Keys)
proc.StandardInput.WriteLine(key+" = "+doxygenProperties[key]);
proc.StandardInput.Close();
proc.WaitForExit();
// Retrieve the app's exit code
int exitCode = proc.ExitCode;
}
当我运行它时会发生什么,我确实没有看到任何新窗口(虽然我认为我应该)并且所有 doxygen.exe 的标准输出都打印到我的应用程序的控制台窗口。
我希望发生的是以下两件事之一:
- Doxygen 在可见窗口中启动,我可以在该窗口中看到它的标准输出,而不是在我的应用程序窗口中。
- Doxygen 在隐藏窗口中启动,并将其标准输出写入日志文件。
我怎样才能实现这些?
此外,为什么我没有为生成的进程获得一个单独的窗口,为什么生成的进程将输出写入我的窗口不是它自己的?
【问题讨论】:
-
所以您没有注意到
RedirectStandardOutput属性或StandardOutput属性,并且您没有检查您正在设置的属性的文档,例如CreateNoWindow其中明确指出是否显示窗口?该文档的存在是有原因的。此外,还讨论了很多重定向输出。我很难相信您很难找到有关该主题的讨论。 -
文档 (msdn.microsoft.com/en-us/library/…) 很糟糕。它对这些事情中的每一个进行了单行解释,而没有解释它们的实际含义。例如,为什么根据我的设置没有出现新窗口,为什么生成的进程的标准输出出现在我的应用程序的窗口中,而不是它自己的?
-
至少并不可怕。 StandardOutput 的文档包含几段文本和多个代码示例。看来您只是认为文档很烂,甚至没有看它。
-
@Servy 文档解释了我如何捕获标准输出,这恰好回答了我的问题的一部分:第 2 点。我没有看到任何解释为什么生成的进程将标准输出写入我的应用程序的标准输出没有任何重定向,或者为什么即使我设置了
start.WindowStyle = ProcessWindowStyle.Normal; start.CreateNoWindow = false;,生成的应用程序也没有显示新窗口