可能重复:
从多个线程调用Console.WriteLine
只是想知道多个线程是否调用相同的console.writeline,它会导致死锁吗?
我的代码的最后一句是Console.WriteLine带有变量的。我希望将变量之间的文本设置""为绿色,将变量设置为红色。
我一直在尝试Console.Foregroundcolor,但这并不成功。
Console.WriteLine("What is your name?");
string name = Console.ReadLine();
Console.WriteLine("Your name is {0}.", name);
Console.ReadKey();
Run Code Online (Sandbox Code Playgroud) 我试图通过以下代码向用户显示警告.但它正在集中输出文本.
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine($"The selected row {selection} is not found in the database.");
Console.ResetColor();
Console.ReadLine();
Environment.Exit(0);
Run Code Online (Sandbox Code Playgroud)
我只想对文本着色"在数据库中找不到所选行"..没什么特别的.否则它看起来很难看.怎么做?
如何获得当前的控制台颜色(前景/背景)?
我有一种方法可以输出改变前景的单行:
public void ColorLine(string line, System.ConsoleColor foreground)
{
// maybe save original foreground color here
// then change it
System.Console.ForegroundColor = foreground;
// write line
System.Console.WriteLine(line);
// set original color
System.Console.ForegroundColor = // original foreground color;
}
Run Code Online (Sandbox Code Playgroud)