如何使用 C# 清除屏幕?

csharpprogrammingserver side programming更新于 2025/5/24 3:37:17

使用 Console.Clear() 方法清除屏幕和控制台缓冲区。调用 Clear 方法时,光标会自动滚动到窗口的左上角。

在这里,我们清除了屏幕,然后设置了 ForegroundColor 和 BackgroundColor −

ConsoleColor newForeColor = ConsoleColor.Blue;
ConsoleColor newBackColor = ConsoleColor.Yellow;

以下是完整代码 −

示例

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      ConsoleColor foreColor = Console.ForegroundColor;
      ConsoleColor backColor = Console.BackgroundColor;
      Console.WriteLine("Clearing the screen!");
      Console.Clear();
      ConsoleColor newForeColor = ConsoleColor.Blue;
      ConsoleColor newBackColor = ConsoleColor.Yellow;
   }
}

输出

Clearing the screen!

相关文章