public class Utils
{
public static void LogDebug(string debuglog)
{
Console.WriteLine($"[Debug] {debuglog}", System.Drawing.Color.Yellow;); //That $ passes the arg(string log) into the string function thus printing it into console
}
public static void InfoLog(string infolog)
{
Console.WriteLine($"[Info] {infolog}", );
}
public static void WarningLog(string warning)
{
Console.WriteLine($"[Warn] {warning}", );
}
}
}
我制作了这段代码来帮助我识别错误和错误,但如果它全是白色的话它并没有真正帮助。这就是为什么我问你是否知道类似于System.Drawing.Color.Yellow;
而不是
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("White on blue.");
Console.WriteLine("Another line.");
更改写入该颜色的所有文本。我只想简单地调用颜色然后再回到白色。
答案 0 :(得分:1)
您可以使用Console.ResetColor()
将控制台重置为默认颜色。然后,我通常会创建一个包含Write
和WriteLine
方法的辅助类,让我自定义颜色:
class ConsoleHelper
{
public static void Write(string message, ConsoleColor foreColor, ConsoleColor backColor)
{
Console.ForegroundColor = foreColor;
Console.BackgroundColor = backColor;
Console.Write(message);
Console.ResetColor();
}
public static void WriteLine(string message, ConsoleColor foreColor, ConsoleColor backColor)
{
Write(message + Environment.NewLine, foreColor, backColor);
}
}
然后,在主程序中,您可以执行以下操作:
private static void Main()
{
Console.Write("If the text is ");
ConsoleHelper.Write("green", ConsoleColor.Green, ConsoleColor.Black);
Console.WriteLine(" then it's safe to proceed.");
Console.Write("\nIf the text is ");
ConsoleHelper.Write("yellow", ConsoleColor.Yellow, ConsoleColor.Black);
Console.Write(" or ");
ConsoleHelper.Write("highlighted yellow", ConsoleColor.White, ConsoleColor.DarkYellow);
Console.WriteLine(" then proceed with caution.");
Console.WriteLine("\nDone!\nPress any key to exit...");
Console.ReadKey();
}
看起来像:
或者,如您的示例所示:
ConsoleHelper.WriteLine("White on blue.", ConsoleColor.White, ConsoleColor.Blue);
Console.WriteLine("Another line.");
产地:
答案 1 :(得分:0)
尝试此功能
static void WriteConsoleAndRestore(string text, ConsoleColor background, ConsoleColor foreground)
{
ConsoleColor currentBackground = Console.BackgroundColor;
ConsoleColor currentForeground = Console.ForegroundColor;
Console.BackgroundColor = background;
Console.ForegroundColor = foreground;
Console.WriteLine(text);
Console.BackgroundColor = currentBackground;
Console.ForegroundColor = currentForeground;
}