[英]c# Console.WriteLine() with color text is massing the output
我试图通过以下代码向用户显示警告。 但它正在集中输出文本。
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);
我只想对文本着色“在数据库中找不到所选行”。 。 没什么特别的。 否则它看起来很难看。 怎么做?
问题是回车和新线为这些线条绘制背景颜色。 只需使用Console.Write()
而不是Console.WriteLine()
:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Red;
// only write without new line
Console.Write($"The selected row {selection} is not found in the database.");
Console.ResetColor();
Console.WriteLine(); // if necessary
Console.ReadLine();
Environment.Exit(0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.