我是这样运行的:

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

...但问题是,带有ffmpeg的控制台立即弹出并消失,因此我无法获得任何反馈.我甚至不知道这个过程是否运行正确.

那么我怎么才能:

  • 告诉控制台保持打开状态

  • 在C#控制台中检索

推荐答案

您需要做的是捕获标准输出流:

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

你可能还需要做一些与StandardError类似的事情.然后你可以用q做你想做的事.

这有点挑剔,就像我在one of my questions年时发现的那样

正如Jon Skeet所指出的,像这样使用字符串连接是不明智的;你应该使用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();

Asp.net相关问答推荐

如何在 ASP.NET RadioButtonList 中的项目之间添加空格

无法为媒体类型application/x-www-form-urlencoded生成样本

如何防止 IISExpress 和我的网站文件夹弄乱我的文档文件夹?

Firefox 和 Chrome 之间 1 像素的行高差

如何将 JQuery 与母版页一起使用?

格式化 DataBinder.Eval 数据

asp.net、url 重写模块和 web.config

aspx 文件中的 if 语句

如何找出我的 Windows 域上托管 LDAP 的服务器?

使用 Moq 验证活动注册

如何获得 System.Diagnostics.Process 的输出?

IIS Express - 增加内存限制

将当前谷歌 map 保存为图片

当.NET抛出WebException((400)错误请求)时如何处理WebResponse?

将列表转换为 json 格式 - 快速简便的方法

在 ASP.NET Web API 2 中禁用 *all* 异常处理(为我自己腾出空间)?

获取 POST 变量

ASP.NET 网格视图与列表视图

目录与目录信息

ASP.NET 自定义控件 - 未知的服务器标记