.NET的Console类大家都不会陌生,但是除了基本的Write和WriteLine之外,我们还可以做一些简单的美化工作,让写出来的内容看起来更舒服。
1)可以改变前景色,背景色
2)可以输入各种符号,需要借助转义字符。 /xdd(加两位16进制数) /udddd(四位16进制数,表示一个unicode字符), 本来还有一个/ddd(三个八进制数,但C#好像不支持。) 另外,C中可以用0前缀表示一个八进制常量数,C#中好像不行。貌似不支持
3)配合格式化字符串
public static void Main()
{
String nl = Environment.NewLine;
String[] colorNames = Enum.GetNames(typeof(ConsoleColor));
Console.WriteLine("{0}All the foreground colors on a constant black background.", nl);
Console.WriteLine(" (Black on black is not readable.){0}", nl);
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 = ConsoleColor.Yellow;
Console.Write("/x01");
Console.Write("/u0001");
Console.Write("/001");
Console.Write("/x10");
Console.Write("/u0010");
Console.Write("/020");
Console.WriteLine();
Console.Write("{0,-50}", "Class1.TestMethod1");
Console.Write("{0,-2}","/x10");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Pass");
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("{0,-50}", "Class1.TestMethod2");
Console.Write("{0,-2}", "/x10");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Failed");
Console.ReadLine();
}