如何在C#中获取当前的控制台前景/背景颜色?

时间:2018-05-19 12:34:47

标签: c# console-application

如何获取当前控制台颜色(前景/背景)?

我有一个方法来输出改变前景的单行:

    public void ColorLine(string line, System.ConsoleColor foreground)
    {
        // maybe save original foreground color here

        // then change it 
        System.Console.ForegroundColor = foreground;

            // write line
            System.Console.WriteLine(line);

        // set original color
        System.Console.ForegroundColor = // original foreground color;
    }

1 个答案:

答案 0 :(得分:3)

// save original foreground color here
ConsoleColor currentForeground = Console.ForegroundColor;


// set original color
Console.ForegroundColor = currentForeground