I'm spawning a child process that runs in a visible console window (it's a batch file that runs MSBuild), and I'd like to have the output generated by the process displayed in the visible console window, as well as capture that output so I can process it in code. I've read several other questions and the MSDN documentation dealing with ProcessStartInfo.RedirectStandardOutput and the like, and I can capture the output from the redirected stream and process it in code just fine:
我生成一个在可见控制台窗口中运行的子进程(它是一个运行MSBuild的批处理文件),我希望将进程生成的输出显示在可见控制台窗口中,并捕获该输出,以便我可以用代码处理它。我已经阅读了一些其他的问题,以及处理ProcessStartInfo的MSDN文档。RedirectStandardOutput等,我可以从重定向流中获取输出,然后用代码很好地处理它:
Process msBuild = new Process();
msBuild.StartInfo.FileName = "Build.bat";
msBuild.StartInfo.UseShellExecute = false;
msBuild.StartInfo.RedirectStandardOutput = true;
msBuild.Start();
string output = msBuild.StandardOutput.ReadToEnd();
msBuild.WaitForExit();
The problem is that the output is not displayed in the console window of the child process; I just get a blank console window on the screen while the process is running, which disappears when it's finished.
问题是在子进程的控制台窗口中没有显示输出;当进程运行时,我在屏幕上得到一个空白的控制台窗口,当进程结束时,这个窗口将消失。
I suppose I could hide the actual child process window, and display a second window that I would simply write the output to as it was captured, but that seems like more work than is necessary. Is there a way to have the output displayed in the console window and still capture it for processing when it's done?
我想我可以隐藏实际的子进程窗口,并显示第二个窗口,我可以简单地将输出写入到它被捕获的过程中,但是这似乎比必要的工作量要多。是否有一种方法可以在控制台窗口中显示输出,并在输出完成后仍将其捕获以进行处理?
2 个解决方案
#1
3
Once you've redirected standard out it's no longer directed at the console. To write to the console you'll have to do it manually.
一旦你重定向了standard out,它就不再指向控制台了。要写入控制台,您必须手动执行。
If you want to display output as the process executes, instead of in 1 big dump at the end, you can use the "OutputDataReceived" event of the Process class.
如果希望在进程执行时显示输出,而不是在最后显示一个大转储,可以使用process类的“OutputDataReceived”事件。
#2
4
Here is what I've used, without using a separate thread:
以下是我所使用的,没有使用单独的线程:
using(System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
proc.EnableRaisingEvents = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Verb = "open";
proc.StartInfo.FileName = "XXXX";
proc.Start();
String sLine = "";
while ((sLine = proc.StandardOutput.ReadLine()) != null)
{
System.Console.WriteLine(sLine);
}
proc.WaitForExit(); //Jon Skeet was here!
errorCode = proc.ExitCode;
proc.Close();
}
#1
3
Once you've redirected standard out it's no longer directed at the console. To write to the console you'll have to do it manually.
一旦你重定向了standard out,它就不再指向控制台了。要写入控制台,您必须手动执行。
If you want to display output as the process executes, instead of in 1 big dump at the end, you can use the "OutputDataReceived" event of the Process class.
如果希望在进程执行时显示输出,而不是在最后显示一个大转储,可以使用process类的“OutputDataReceived”事件。
#2
4
Here is what I've used, without using a separate thread:
以下是我所使用的,没有使用单独的线程:
using(System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
proc.EnableRaisingEvents = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Verb = "open";
proc.StartInfo.FileName = "XXXX";
proc.Start();
String sLine = "";
while ((sLine = proc.StandardOutput.ReadLine()) != null)
{
System.Console.WriteLine(sLine);
}
proc.WaitForExit(); //Jon Skeet was here!
errorCode = proc.ExitCode;
proc.Close();
}