如何在控制台中显示进程的同时获取进程的控制台输出

时间:2016-03-31 05:38:46

标签: c# unity3d

我的Unity应用程序启动了一个System.Diagnostics.Process(ffmpeg),它打开了一个控制台窗口,用于写入其输出。

我已经想出如何重定向输出,以便我可以在我的Unity应用程序中读取它,但这样做可以防止它在控制台窗口中显示任何内容。

有没有办法从进程中读取控制台输出并让它同时显示在控制台窗口中?

编辑:这是我的代码。

var processInfo = new System.Diagnostics.ProcessStartInfo("ffmpeg.exe", command);
processInfo.UseShellExecute = false;
processInfo.WorkingDirectory = System.Environment.CurrentDirectory;
processInfo.ErrorDialog = true;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;

_StreamerProcess = new System.Diagnostics.Process();
_StreamerProcess.StartInfo = processInfo;
_StreamerProcess.EnableRaisingEvents = true;
_StreamerProcess.OutputDataReceived += _StreamerProcess_OutputDataReceived;
_StreamerProcess.ErrorDataReceived += _StreamerProcess_ErrorDataReceived;
_StreamerProcess.Start();
_StreamerProcess.BeginOutputReadLine();
_StreamerProcess.BeginErrorReadLine();

_StreamerProcess_OutputDataReceived和_StreamerProcess_ErrorDataReceived现在只需调用Unity的Debug.Log,我就可以让它工作了。

1 个答案:

答案 0 :(得分:1)

ProcessStartInfo.CreateNoWindow设为false

在这种情况下,它应该是

processInfo.CreateNoWindow = false;

你的整个代码:

var processInfo = new System.Diagnostics.ProcessStartInfo("ffmpeg.exe", command);
processInfo.UseShellExecute = false;
processInfo.CreateNoWindow = false;/////////////////Changed here
processInfo.WorkingDirectory = System.Environment.CurrentDirectory;
processInfo.ErrorDialog = true;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;

_StreamerProcess = new System.Diagnostics.Process();
_StreamerProcess.StartInfo = processInfo;
_StreamerProcess.EnableRaisingEvents = true;
_StreamerProcess.OutputDataReceived += _StreamerProcess_OutputDataReceived;
_StreamerProcess.ErrorDataReceived += _StreamerProcess_ErrorDataReceived;
_StreamerProcess.Start();
_StreamerProcess.BeginOutputReadLine();
_StreamerProcess.BeginErrorReadLine();

编辑:默认忘记CreateNoWindow false。我认为这里的问题是你如何创建ProcessStartInfo。不要使用构造函数来解析文件名。使用ProcessStartInfo.FileName作为文件名,然后使用ProcessStartInfo.Arguments作为参数。

从我的某个应用中删除了这个。试试吧。它适用于我。

System.Diagnostics.Process process = null;

   void runCommand(string commandFileName, string arg)
    {
        //Debug.Log("Full command: " + arg);
        process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

        //No Windows
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = false; //Optional

        //Redirect to get response
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;

        startInfo.FileName = commandFileName;
        startInfo.Arguments = arg;
        process.StartInfo = startInfo;

        process.EnableRaisingEvents = true;
        process.OutputDataReceived += _StreamerProcess_OutputDataReceived;
        process.ErrorDataReceived += _StreamerProcess_ErrorDataReceived;

        process.Start();

        process.BeginOutputReadLine();
        process.BeginErrorReadLine();
    }