首页 > 解决方案 > 控制台中的一行上有多种颜色

问题描述

我正在做一个项目,为此我想显示 CPU 架构是 64 位还是 32 位。现在,我已经有了:

 bool is64 = Environment.Is64BitOperatingSystem;
 if (is64) { Console.WriteLine("Architecture: 64 bit"); }
 else { Console.WriteLine("Architecture: 32 bit"); }

但我想将“架构”显示为白色,将行的 64 位或 32 位部分显示为绿色。那可能吗?如果是这样,如果我能得到一个如何做的例子,我将不胜感激。

编辑

人们误解了这个问题。我的意思是这样的:

在此处输入图像描述

任何人?

标签: c#

解决方案


Simple:

You set the color to White then Use Console.Write to output "Architecture".
Then, Set the color to Green and use Console.WriteLine to output the bit part.

Console.ForegroundColor = ConsoleColor.White;
Console.Write("Architecture: ");
Console.ForegroundColor = ConsoleColor.Green;
bool is64 = Environment.Is64BitOperatingSystem;
if (is64) { Console.WriteLine("64 bit"); }
else { Console.WriteLine("32 bit"); }

You might want save the original ForegroundColor aside, so you can restore it after.


推荐阅读