How can I change the text color in the windows command prompt

后端 未结 14 2669
南旧
南旧 2020-12-14 22:32

I have a command line program, which outputs logging to the screen.

I want error lines to show up in red. Is there some special character codes I can output to switc

14条回答
  •  感动是毒
    2020-12-14 22:57

    You can read here a good and illustrated article: http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/

    I think setting console text color is pretty language-specific. Here is an example in C# from MSDN:

    for (int x = 0; x < colorNames.Length; x++)
    {
      Console.Write("{0,2}: ", x);
      Console.BackgroundColor = ConsoleColor.Black;
      Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);
      Console.Write("This is foreground color {0}.", colorNames[x]);
      Console.ResetColor();
      Console.WriteLine();
    }
    

    Console.ForegroundColor is the property for setting text color.

提交回复
热议问题