是否可以使命令输出提示不同的颜色? C#编程
问题描述:
假设你想用C#编写一个程序并用命令提示符编译它。假设简单的程序只说;是否可以使命令输出提示不同的颜色? C#编程
Console.WriteLine("a" + "b" + "c" + "d");
是否可以在命令提示符下以不同颜色打印a,b,c,d?
答
是的。你必须改变颜色和打印的文字,如:
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("a");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("b");
.
...
答
的Console
类具有ForegroundColor
属性,您可以使用:
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(a);
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(b);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(c);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(d);
答
使用Console.ForegroundColor
财产与ConsoleColor
枚举。
获取或设置控制台的前景色。
赞;
public static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.White;
string a = "a";
Console.WriteLine(a);
Console.ForegroundColor = ConsoleColor.Blue;
string b = "b";
Console.WriteLine(b);
Console.ForegroundColor = ConsoleColor.DarkGreen;
string c = "c";
Console.WriteLine(c);
Console.ForegroundColor = ConsoleColor.Red;
string d = "d";
Console.WriteLine(d);
}
输出将是;
http://stackoverflow.com/questions/7937256/changing-text-color-in-c-sharp-console-application但是,你需要改变颜色和WriteLine后写各“d”。 – kenny 2013-02-15 22:06:30