【问题标题】:How to get current console foreground/background colors in C#? [duplicate]如何在 C# 中获取当前控制台前景色/背景色? [复制]
【发布时间】:2018-10-29 17:22:19
【问题描述】:

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

我有一种方法可以输出具有更改前景的单行:

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

【问题讨论】:

  • 使用Console.ForegroundColor并将其存储在变量中,例如var currentColor = Console.ForegroundColor;

标签: c# console-application


【解决方案1】:
// save original foreground color here
ConsoleColor currentForeground = Console.ForegroundColor;


// set original color
Console.ForegroundColor = currentForeground 

【讨论】:

    猜你喜欢
    • 2012-01-24
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    相关资源
    最近更新 更多