【发布时间】:2015-03-05 15:26:07
【问题描述】:
我遇到了控制台输出重定向问题(用 C# 编写)。 使用“cmd.exe”和任何参数(如“dir”)一切正常,但使用“gnatmake.exe”或“gcc.exe”没有!我没有看到输出并且我给这些程序的命令不起作用(源代码没有编译)!我试过带参数和不带参数 - 什么都没有!
console = new Process();
// The path is correct, I've checked!
console.StartInfo.FileName = @"D:\MinGW\bin\gnatmake.exe";
// cmd.exe works perfectly!
//console.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
// gnatmake.exe isn't working even without arguments
console.StartInfo.Arguments = currFile;
console.StartInfo.UseShellExecute = false;
console.StartInfo.CreateNoWindow = true;
console.StartInfo.RedirectStandardOutput = true;
console.OutputDataReceived += new DataReceivedEventHandler(ConsoleOutputHandler);
console.Start();
console.BeginOutputReadLine();
void ConsoleOutputHandler(object sendingProcess, DataReceivedEventArgs recieved)
{
if (!string.IsNullOrWhiteSpace(recieved.Data))
{
MessageBox.Show(recieved.Data);
}
}
我尝试了其他方法:
console = new Process();
console.StartInfo.FileName = @"D:\MinGW\bin\gnatmake.exe";
console.StartInfo.UseShellExecute = false;
console.StartInfo.CreateNoWindow = true;
console.StartInfo.RedirectStandardOutput = true;
console.StartInfo.RedirectStandardInput = true;
console.Start();
StreamWriter sr = console.StandardInput;
sr.WriteLine(currFile);
sr.Close();
string str = console.StandardOutput.ReadToEnd();
console.WaitForExit();
MessageBox.Show(str);
仍然不能与“gnatmake.exe”一起使用,但可以与“cmd.exe”一起使用!
但是我刚刚写了这个:
Process.Start(@"D:\MinGW\bin\gnatmake.exe", currFile);
它成功了,编译了文件,但是用这个函数我不能有输出! “gnatmake.exe”和“gcc.exe”有什么问题?如何正确地做到这一点? 感谢您的回答!
【问题讨论】:
-
不设置
CreateNoWindow = true是否有效? -
不起作用!我试图评论这一行并将 true 替换为 false - 什么都没有!
-
如果您在命令行中使用重定向输出运行
gnatmake.exe会发生什么,即使用命令gnatmake.exe | more? -
我试过这个
gnatmake.exe hello.adb > test.txt。文件test.txt已创建但为空!源代码编译正确,我仍然在命令行窗口中看到输出。 -
听起来
gnatmake是直接写入控制台而不是尊重标准输出。您实际上对此无能为力,尽管由于这是开源的,我想您可以自己修复它,如果它对您来说足够重要以证明努力是合理的。见stackoverflow.com/questions/19915834/…