这对我有用——现在发布这个,因为我希望我能早点找到它。请注意,这只是从真实代码中提取的片段,因此可能存在细微错误。
该技术基于一些 MSDN 代码。我无法弄清楚的是如何让输出窗口“即时”更新。在此任务返回之前它不会更新。
// Set this to your output window Pane
private EnvDTE.OutputWindowPane _OutputPane = null;
// Methods to receive standard output and standard error
private static void StandardOutputReceiver(object sendingProcess, DataReceivedEventArgs outLine)
{
// Receives the child process' standard output
if (! string.IsNullOrEmpty(outLine.Data)) {
if (_OutputPane != null)
_OutputPane.Write(outLine.Data + Environment.NewLine);
}
}
private static void StandardErrorReceiver(object sendingProcess, DataReceivedEventArgs errLine)
{
// Receives the child process' standard error
if (! string.IsNullOrEmpty(errLine.Data)) {
if (_OutputPane != null)
_OutputPane.Write("Error> " + errLine.Data + Environment.NewLine);
}
}
// main code fragment
{
// Start the new process
ProcessStartInfo startInfo = new ProcessStartInfo(PROGRAM.EXE);
startInfo.Arguments = COMMANDLINE;
startInfo.WorkingDirectory = srcDir;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
Process p = Process.Start(startInfo);
p.OutputDataReceived += new DataReceivedEventHandler(StandardOutputReceiver);
p.BeginOutputReadLine();
p.ErrorDataReceived += new DataReceivedEventHandler(StandardErrorReceiver);
p.BeginErrorReadLine();
bool completed = p.WaitForExit(20000);
if (!completed)
{
// do something here if it didn't finish in 20 seconds
}
p.Close();
}