Reputation: 27
I have some text printed in my C# console (Lots of it) and I am wondering what would be the simplest way to change the text color all at once without clearing the console and reprinting it in the new color, sort of having the same effect as System(color ##) command in C++...Thanks in advance.
Upvotes: 0
Views: 1991
Reputation: 39102
You can change the console output color using Console.BackgroundColor
and Console.ForegroundColor
properties. After you are done writing in the new color, use Console.ResetColor()
to go back to defaults.
Changing the colors after the fact is a problem, because C# has no direct way to read text at a given position.You can rewrite it however, if you know what exactly is there, in a different color (first jumping to the location using Console.SetCursorPosition
method and then writing over the original text).
If you want to be as efficient as possible, you will need a higher caliber in form of some P/Invoke wizadry. This is quite well described in the accepted answer to this similar question. The solution there takes advantage of writing the entire Console
buffer at once, which is very fast.
Upvotes: 1