Is it possible to write to the console in colour in .NET?
Solution 1
Yes. See this article. Here's an example from there:
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("White on blue.");
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.
Solution 3
Above comments are both solid responses, however note that they aren't thread safe. If you are writing to the console with multiple threads, changing colors will add a race condition that can create some strange looking output. It is simple to fix though:
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();
}
}
}
Solution 4
I've created a small plugin (available on NuGet) that allows you to add any (if supported by your terminal) color to your console output, without the limitations of the classic solutions.
It works by extending the String
object and the syntax is very simple:
"colorize me".Pastel("#1E90FF");
Both foreground and background colors are supported.
Solution 5
Yes, it's easy and possible. Define first default colors.
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Console.Clear()
it's important in order to set new console colors. If you don't make this step you can see combined colors when ask for values with Console.ReadLine()
.
Then you can change the colors on each print:
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Red text over black.");
When finish your program, remember reset console colors on finish:
Console.ResetColor();
Console.Clear();
Now with netcore we have another problem if you want to "preserve" the User experience because terminal have different colors on each Operative System.
I'm making a library that solves this problem with Text Format: colors, alignment and lot more. Feel free to use and contribute.
https://github.com/deinsoftware/colorify/ and also available as NuGet package
Related videos on Youtube
Comments
-
NibblyPig almost 2 years
Writing a small command line tool, it would be nice to output in different colours. Is this possible?
-
RollRoll over 7 yearsis it possible to add background image?
-
Joe over 6 yearsWriting from multiple threads without synchronisation will result in garbled output even if there are no color changes: it's not just changing colors that causes a race condition
-
BatteryBackupUnit about 6 years@Joe A single
Write
call will not be garbled. See stackoverflow.com/questions/4812508/…. It's just the order of the write-calls which is not "safe". -
Remigiusz Schoida over 4 years@RollRoll not in the default cmd.exe. You can try using a Console emulator though, which supports background image tweaking, but still, it cannot be done programatiaclly anyway.
-
Kyle Delaney about 3 yearsBut what about if other processes are writing to the same console? They won't be using your lock object. Is there any truly atomic way to change the color, write to the console, and then change the color back?
-
Roger Hill about 3 yearsThey won't be using the console writer if you don't choose to use the console writer. The implication here is that you use this object for all writes to the console. I suppose you could do some tricky wrapper on the Console.Write command, but that sees a bit overkill.
-
Pang about 3 yearsI believe this library uses those ANSI escape color codes, which appears to be what Microsoft.Extensions.Logging.Console is also using.
-
silkfire about 3 years@Pang Microsoft.Extensions.Logging.Console is a logging library, whereas Pastel can be used for printing directly to the console; also it seems to be supporting a limited subset of colors from what I can derive from the source code.
-
Tono Nam almost 2 years@Pang that library does not implement all foreground colors for some reason. I tend to go with Microsoft solutions but the Pastel solution is more complete.
-
KarloX over 1 yearThanks - Quite nice. But I can't use is due to license concerns.
-
Ramin Rahimzada over 1 yearDon't be concerned. Everyone can use it in anywhere without blaming me for future issues that may occur in production usage
-
Jay Imerman over 1 yearI honestly don't know why this isn't upvoted a hundred times! This is really great, a nice elegant solution.