如何使用 C# 清除螢幕?


使用 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!

更新於: 2020-06-22

10 千+ 瀏覽

開啟 職業生涯

透過完成課程獲取認證

入門
廣告