MSDN声明在.NET中可以捕获进程的输出并且同时在控制台窗口中显示它。
通常在设置StartInfo.RedirectStandardOutput = true时;控制台窗口保持空白。 由于MSDN网站没有为此提供样本,我想知道是否有人会有样本或可以指向我的样本?
当进程将文本写入其中时 标准流,该文本通常是 显示在控制台上。通过 重定向StandardOutput 流,你可以操纵或压制 流程的输出。例如, 你可以过滤文本,格式化它 不同的是,或写入输出 控制台和指定的日志 文件。 MSDN
顺便说一下,这篇文章与Capture standard output and still display it in the console window类似。但那篇文章并没有结束工作样本。
非常感谢,
帕特里克
答案 0 :(得分:1)
您可以使用
轻松捕获所有消息Process build = new Process();
...
build.StartInfo.UseShellExecute = false;
build.StartInfo.RedirectStandardOutput = true;
build.StartInfo.RedirectStandardError = true;
build.StartInfo.CreateNoWindow = true;
build.ErrorDataReceived += build_ErrorDataReceived;
build.OutputDataReceived += build_ErrorDataReceived;
build.EnableRaisingEvents = true;
...
并创建Event build_ErrorDataReceived
static void build_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
string msg = e.Data;
if (msg != null && msg.Length > 0)
{
// in msg you have the line you need!
}
}
我添加了一个小例子
Solution Files(VS 2008)
答案 1 :(得分:0)
在SO上看到这个答案,我发布了一个进程代码,用于执行netstat将输出流重定向到Stringbuilder实例。该过程创建一个隐藏的窗口,不可见......
您可以通过分别更改值来轻微修改代码
ps.CreateNoWindow = true; <--- Comment this out... ps.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; <--- Comment this out...