简体   繁体   中英

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

I'm currently compiling a C program through my C# app. If gcc outputs anything it means that there was an error. 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.

I would bet that it's writing to StandardError instead.

    RedirectStandardError = true,

    ....

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

Use Console.WriteLine("message"); if you are working with the console. The MessageBox is for WinForms applications.

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

Console.WriteLine writes to the standard output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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