本文介绍了如何获得的System.Diagnostics.Process的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像这样运行的ffmpeg:

I run ffmpeg like this:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo(ffmpegPath, myParams);
p.Start();
p.WaitForExit();

......但问题是,与ffmpeg的控制台弹出并消失向右走,所以我不能得到任何反馈。我甚至不知道是否正确流程运行。

... but the problem is that the console with ffmpeg pops up and disappears right away, so I can't get any feedback. I don't even know if the process ran correctly.

那么,怎样才能我要么:

So how can I either:


  • 告诉控制台保持打开

  • Tell the console to stay opened

找回在C#什么控制台
显示

Retrieve in the C# what the consoledisplayed

推荐答案

您需要做的就是捕捉到标准输出流什么:

What you need to do is capture the Standard Output stream:

p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
// instead of p.WaitForExit(), do
string q = "";
while ( ! p.HasExited ) {
    q += p.StandardOutput.ReadToEnd();
}

您可能还需要做 StandardError的类似的东西。然后,你可以做你想要什么

You may also need to do something similar with StandardError. You can then do what you wish with q.

这是一个有点挑剔,正如我在我的问题之一

It is a bit finicky, as I discovered in one of my questions

由于乔恩斯基特所指出的,这是不聪明的表现,明智的做法是使用字符串连接这样的;您应该使用<$c$c>StringBuilder:

As Jon Skeet has pointed out, it is not smart performance-wise to use string concatenation like this; you should instead use a StringBuilder:

p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
// instead of p.WaitForExit(), do
StringBuilder q = new StringBuilder();
while ( ! p.HasExited ) {
    q.Append(p.StandardOutput.ReadToEnd());
}
string r = q.ToString();

这篇关于如何获得的System.Diagnostics.Process的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 17:56