写一个小的命令行工具,用不同的颜色输出会很好。这可能吗?


当前回答

class Program
{
    static void Main()
    {
        Console.BackgroundColor = ConsoleColor.Blue;
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("White on blue.");
        Console.WriteLine("Another line.");
        Console.ResetColor();
    }
}

从这里拍的。

其他回答

是的,可能如下所示。这些颜色可以在控制台应用程序中使用,以红色等方式查看一些错误。

Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;//after this line every text will be white on blue background
Console.WriteLine("White on blue.");
Console.WriteLine("Another line.");
Console.ResetColor();//reset to the defoult colour
class Program
{
    static void Main()
    {
        Console.BackgroundColor = ConsoleColor.Blue;
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("White on blue.");
        Console.WriteLine("Another line.");
        Console.ResetColor();
    }
}

从这里拍的。

我只是想在使用console。writeline()时调整文本颜色; 所以我不得不写

Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("my message");
Console.ResetColor();

每次我想写点什么

所以我发明了我的WriteLine()方法,并一直在Program类中使用它而不是console。

public static void WriteLine(string buffer, ConsoleColor foreground = ConsoleColor.DarkGreen, ConsoleColor backgroundColor = ConsoleColor.Black)
{
   Console.ForegroundColor = foreground;
   Console.BackgroundColor = backgroundColor;
   Console.WriteLine(buffer);
   Console.ResetColor();
}

为了让它更简单,我还写了一个Readline()方法,像这样:

public static string ReadLine()
{
   var line = Console.ReadLine();
   return line ?? string.Empty;
}

现在我们要做的就是在控制台中写入或读取一些东西:

static void Main(string[] args) {
   WriteLine("hello this is a colored text");
   var answer = Readline();
}

我已经创建了一个小插件(在NuGet上可用),允许您添加任何(如果您的终端支持)颜色到您的控制台输出,而不受经典解决方案的限制。

它通过扩展String对象来工作,语法非常简单:

"colorize me".Pastel("#1E90FF");

前景色和背景色都受支持。

上面的注释都是可靠的响应,但是注意它们不是线程安全的。如果您正在用多个线程写入控制台,更改颜色将添加一个竞争条件,可能会产生一些奇怪的输出。不过修复起来很简单:

public class ConsoleWriter
{
    private static object _MessageLock= new object();

    public void WriteMessage(string message)
    {
        lock (_MessageLock)
        {
            Console.BackgroundColor = ConsoleColor.Red;
            Console.WriteLine(message);
            Console.ResetColor();
        }
    }
}