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);
  }
  else
  {
      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.

3 Replies to “C# Custom Colors in Consoles”

  1. Good hint, but it would be better if the user could set his own color based on rgb values. Any idea how to set ForeGround and BackGround color of the Console to user defined rgb values? Not the Console colors which resemble user defined rgb values, but the exact rgb desired by the user. Is there anyway to achieve this effect?

Leave a Reply

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