在途中更改文本颜色?

时间:2020-07-22 14:24:14

标签: c# syntax

所以基本上,我只想使一个单词变成黄色,其余的变成白色,所以我要做的是:

Console.Write("Words ");
Console.ForegroudColor = ConsoleColor.Yellow;
Console.Write("Words ");
Console.ForegroudColor = ConsoleColor.White;
Console.Write("Words ");

有没有一种方法可以使它更容易完成?

EX :(不起作用)

Console.Write("Words " + Console.ForegroudColor = ConsoleColor.Yellow; + "Words");

1 个答案:

答案 0 :(得分:1)

没有内置的东西-但是我在GitHub上有一个名为ExtendedConsole的开源项目,它使用简单的基于xml的标记即可做到这一点。

The documentation is hosted on GitHub pages,您可以将其下载为nuget package

并像这样使用它:

exConsole.Write("Somw words here <c f='yellow'>This is yellow!</c> some more words here");

它还包含许多其他东西,可以使您在编写控制台应用程序(例如菜单和读取方法)时更加轻松。