C# 如何在控制台中显示进程的控制台输出
我的Unity应用程序启动System.Diagnostics.Process(ffmpeg),它将打开一个控制台窗口,在其中写入输出 我已经知道了如何重定向输出,以便可以在Unity应用程序中读取它,但这样做会阻止它在控制台窗口中显示任何内容 有没有一种方法可以读取进程的控制台输出,同时让它仍然显示在其控制台窗口中 编辑:这是我的代码C# 如何在控制台中显示进程的控制台输出,c#,unity3d,C#,Unity3d,我的Unity应用程序启动System.Diagnostics.Process(ffmpeg),它将打开一个控制台窗口,在其中写入输出 我已经知道了如何重定向输出,以便可以在Unity应用程序中读取它,但这样做会阻止它在控制台窗口中显示任何内容 有没有一种方法可以读取进程的控制台输出,同时让它仍然显示在其控制台窗口中 编辑:这是我的代码 var processInfo = new System.Diagnostics.ProcessStartInfo("ffmpeg.exe", command)
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\u OutputDataReceived和\u StreamerProcess\u ErrorDataReceived在我启动Unity的Debug.Log时,请暂时调用它。将
ProcessStartInfo.CreateNowInow
设置为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();
}
有办法。先发布你的代码,然后你会有办法…@Programmer-我在问题中添加了我的代码。CreateNoWindow默认为false。重定向输出并不会阻止窗口出现,它只是在Unity应用程序获取输出时保持空白。修改了我的答案。从“编辑”往下看。单独分配文件和参数对我来说似乎没有什么区别。在将它们传递给构造函数时,您是否真的得到了与我相同的结果?你是在团结一致吗?我以前遇到过Unity的古老mono版本存在常规.NET环境所没有的问题。我给你的代码就是我在个人编辑器插件中使用的代码。如果有帮助的话,我正在使用unity 5.4。这个代码对我有用。我用它与谷歌眼镜(Android设备)通信,它的工作原理与预期一致。我可以通过将
CreateNoWindow
设置为true来隐藏黑屏,或者通过将其设置为false
来显示黑屏。上面的代码和我的代码之间的区别在于,我没有使用启用RaisingEvents、OutputDataReceived、ErrorDataReceived、BeginOutputReadLine
和BeginErrorReadLine
。我将进程
公开,以便我可以使用if(!process.HasExited)从更新函数访问它
然后处理.StandardOutput.read
逐字节读取内容。在研究我的插件时,我还从其他地方读到,Unity中进程中的事件存在问题。这就是为什么我没有用它。