C# Custom Colors in Consoles

Ever wanted to make colored lines in a Console Application? By this I mean, for example something went wrong and shows up “An error occurred! Please try again.”. Let’s say you want only errors be colored red and when sothing succeded it would be green, like “Success!”. It is indeed really simple to get this working as I explain in this post titled “ Custom Colors in Consoles”.

I will make a speciffic example for this post:

  String code = "";

  Console.Write("Enter code to continue: ", Console.ForegroundColor = ConsoleColor.White);
  if (code != "mycode")
      Console.Write("\nFalse Code!", Console.ForegroundColor = ConsoleColor.Red);
      Console.Write("\nSuccess", Console.ForegroundColor = ConsoleColor.Green);

As you have probably noticed I used this:

Console.ForegroundColor = ConsoleColor.Red

And this is basically what changes the color.

I hope this post helped you. Please leave a comment.

2 Replies to “C# Custom Colors in Consoles”

Leave a Reply

Your email address will not be published. Required fields are marked *