为什么这个方法没有重定向我的输出.exe [ffmpeg]?

Exi*_*tos 5 c# redirect ffmpeg console-application

我有方法:

public static string StartProcess(string exePathArg, string argumentsArg, int timeToWaitForProcessToExit)
    {
        string retMessage = "";

        using (Process p = new Process())
        {
            p.StartInfo.FileName = exePathArg;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.Arguments = argumentsArg;
            p.StartInfo.UseShellExecute = false;



            try
            {
                p.Start();
                StreamReader myOutput = p.StandardOutput;

                retMessage = "STANDARD OUTPUT: " +  myOutput.ReadToEnd();

                p.WaitForExit(timeToWaitForProcessToExit);
            }
            catch (Exception ex)
            { 
                retMessage = "EXCEPTION THROWN: " + ex.ToString();

            }
            finally
            {
                try
                {
                    p.Kill();
                }
                catch { }
            }
        }

        return retMessage;
    }
Run Code Online (Sandbox Code Playgroud)

但它并没有将我的输出重定向到retMessage.任何想法?我在bat文件中测试了参数,输出肯定是输出.

干杯,皮特

Ale*_*lex 8

我的猜测(同意dtb的评论):AFAIK ffmpeg使用stdout来输出二进制数据(多媒体,快照等),stderr用于记录目的.在您的示例中,您使用stdout.

所以,将代码更改为:

    p.StartInfo.RedirectStandardError = true;
    ...
    string log = p.StandardError.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)

它应该解决你的问题.