簡體   English   中英

通過C#運行時從命令行獲取GCC的輸出

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

我目前正在通過C#應用程序編譯C程序。 如果gcc輸出任何內容,則表示存在錯誤。 我想檢索此錯誤並將其顯示給用戶。

目前,我正在這樣做...

        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);

但是,即使程序中出現錯誤,也不會將任何內容重定向到標准輸出,並且EndOfStream始終返回true。

我敢打賭,它是寫給StandardError

    RedirectStandardError = true,

    ....

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

使用Console.WriteLine("message"); 如果您正在使用控制台。 MessageBox用於WinForms應用程序。

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

Console.WriteLine寫入標准輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM