【问题标题】:Console output redirect issue控制台输出重定向问题
【发布时间】: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/…

标签: c# windows gcc gnat


【解决方案1】:

我已经完成了这项工作!我为gnatmake 找到了参数-eS,它在命令行中工作(gnatmake.exe -eS hello.adb > test.txt)。 出于某种原因(谁能告诉我为什么?)它在这段代码中不起作用:

console = new Process();
console.StartInfo.FileName = @"D:\MinGW\bin\gnatmake.exe";
console.StartInfo.UseShellExecute = false;
console.StartInfo.CreateNoWindow = false;
console.StartInfo.RedirectStandardOutput = true;
console.StartInfo.RedirectStandardInput = true;
console.Start();

StreamWriter sw = console.StandardInput;
sw.WriteLine("-eS " + currFile);
sw.Close();

string str = console.StandardOutput.ReadToEnd();
console.WaitForExit();
MessageBox.Show(str);

但它在没有重定向标准输入的情况下工作(也许输入根本不是标准的,以及输出?):

console = new Process();
console.StartInfo.FileName = @"D:\MinGW\bin\gnatmake.exe";
console.StartInfo.Arguments = "-eS " + currFile;
console.StartInfo.UseShellExecute = false;
console.StartInfo.CreateNoWindow = false;
console.StartInfo.RedirectStandardOutput = true;
console.Start();

string str = console.StandardOutput.ReadToEnd();
console.WaitForExit();
MessageBox.Show(str);

所以,我至少对我的一个问题有了答案。感谢Harry Johnston 的回复!

更新:
我已经意识到,这段代码只给了我gnatmake 的输出。 gnatmake 正在为它的工作调用另一个程序。但我找到了一个简单的解决方案!
gnatmake hello.adb 2> test.txt 正在做我需要的事情。我可以从 test.txt 获得输出。
我读到2> 正在获得stderr 输出。我试图在 C# 中获得错误输出,但我得到的只是gnatmake 的参数列表!我想我不想打扰它,从文件中读取文本对我来说已经足够了。

【讨论】:

    猜你喜欢
    • 2013-11-14
    • 1970-01-01
    • 2015-03-10
    • 2015-09-11
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多