简体   繁体   English

通过C#运行时从命令行获取GCC的输出

[英]Getting GCC's output from command line when running it through C#

I'm currently compiling a C program through my C# app. 我目前正在通过C#应用程序编译C程序。 If gcc outputs anything it means that there was an error. 如果gcc输出任何内容,则表示存在错误。 I want to retrieve this error and display it to the user. 我想检索此错误并将其显示给用户。

Currently, I'm doing this... 目前,我正在这样做...

        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = _cCompilerPath,
                Arguments = " ./file.c",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true                    
            }
        };

        proc.Start();
        string message = string.Empty;
        while (!proc.StandardOutput.EndOfStream)
        {
            string line = proc.StandardOutput.ReadLine();
            message = string.Concat(message, line);      
        }
        MessageBox.Show("OUTPUT :" + message);

But even when there is an error in the program, nothing gets redirected to the Standard Output, and the EndOfStream always returns true. 但是,即使程序中出现错误,也不会将任何内容重定向到标准输出,并且EndOfStream始终返回true。

I would bet that it's writing to StandardError instead. 我敢打赌,它是写给StandardError

    RedirectStandardError = true,

    ....

    while (!proc.StandardError.EndOfStream)
    {
        string line = proc.StandardError.ReadLine();
        message = string.Concat(message, line);      
    }

Use Console.WriteLine("message"); 使用Console.WriteLine("message"); if you are working with the console. 如果您正在使用控制台。 The MessageBox is for WinForms applications. MessageBox用于WinForms应用程序。

Console.WriteLine("OUTPUT: {0}", message);

Console.WriteLine writes to the standard output. Console.WriteLine写入标准输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM