Coder Perfect

In.NET, is it possible to write to the console in color? ?

Problem

It would be wonderful to output in different colors when writing a tiny command line utility. Is that even possible?

Asked by NibblyPig

Solution #1

Yes. Take a look at this article. Here’s an illustration of what I mean:

Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("White on blue.");

Answered by Mark Byers

Solution #2

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

Taken from here.

Answered by Darin Dimitrov

Solution #3

Both of the above responses are excellent, but they are not thread safe. If you’re using multiple threads to write to the terminal, changing colors will cause a race condition, which can result in weird output. It is, however, straightforward to correct:

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

Answered by Roger Hill

Solution #4

I’ve written a simple plugin (available on NuGet) that allows you to add any color to your console output (if supported by your terminal), without the constraints of traditional solutions.

It operates by extending the String object, with the following syntax:

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

Colors in the foreground and background are both supported.

Answered by silkfire

Solution #5

Yes, that is simple and feasible. Create the first set of default colors.

Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();

Console. Clear() It’s necessary in order to change the console’s colors. If you skip this step, you’ll get mixed colors when using Console to get values. ReadLine().

The colors of each print can then be changed:

Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Red text over black.");

When you’ve finished your program, remember to reset the terminal colors:

Console.ResetColor();
Console.Clear();

Now, with Netcore, we face a new problem if we wish to “preserve” the user experience, because terminals on different operating systems have different colors.

I’m working on a library that uses Text Format to handle this problem: colors, alignment, and more. You are welcome to use and contribute.

https://github.com/deinsoftware/colorify/ as well as being accessible as a NuGet package

Windows/Linux (Dark) Colors:

Colors (Light) for MacOS:

Answered by equiman

Post is based on https://stackoverflow.com/questions/2743260/is-it-possible-to-write-to-the-console-in-colour-in-net