彩色控制台输出

4
我刚开始学习C#,想知道在终端中是否有ASCII的方式可以获得彩色输出,例如:

在Ruby中,我可以这样做:

puts "\e[32mThis will be green\e[0m"

在JavaScript中,我可以这样做:

function say(input){ 
    console.log("\033[32m" + input + "\033[0m"
}

say("This will be green")

我该如何在C#中做同样的事情?


当然,看下面我的回答。 - fulvio
2个回答

3
你可以使用以下的C#代码:
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("White on blue.");
Console.Read();

请记住,您也可以在JavaScript中执行此操作:

console.log('%cHello world', 'background-color: #0000FF; color: #FFFFFF');

运行代码片段,然后查看JavaScript控制台。


2
在控制台应用程序中,您可以执行以下操作:
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Hi I'm green");
Console.Read();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接