我正在尝试与使用Fortran编写的旧CLI应用程序进行交互。在C#中使用CLI应用程序的标准输出始终为空
问题是,我甚至无法读取程序的输出。阅读各种SO问题我试了下面的代码:
// Start a new process
Process p = new Process();
// Redirect output and error
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
// Exe path
p.StartInfo.FileName = @"C:\path\to\exe";
// Hook received events
p.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data);
p.ErrorDataReceived += (sender, e) => Console.WriteLine(e.Data);
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
但是..没有任何反应。使用System32文件夹中的通用ipconfig.exe替换Fortran exe实际上可以工作,并且我可以读取输出。
更新:而在VS中来回测试我注意到,当我关闭窗口时,正确的输出显示几分之一秒。 看来,输出在那里,但直到我关闭窗口才显示。
我错过了什么吗?
为什么'p.OutputDataReceived + =(sender,e)=> Console.WriteLine(e.Data);'重复两次? – mjwills
ErrorDataReceived,希望。有一些脾气暴躁的程序也需要RedirectStandardInput = true才能正常工作,xcopy.exe是一个臭名昭着的例子。接下来要尝试。 –
@mjwills哎呀,它是'ErrorDataReceived'。对不起。 @HansPassant谢谢,会试一试。编辑:尝试,不幸的是没有改变,但我注意到了一些。请参阅更新中的问题。 –