Console.ForegroundColor 屬性

定義

取得或設定主控台的前景色彩。

C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static ConsoleColor ForegroundColor { get; set; }
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static ConsoleColor ForegroundColor { get; set; }
C#
public static ConsoleColor ForegroundColor { get; set; }

屬性值

ConsoleColor

ConsoleColor,可指定主控台的前景色彩,也就是出現的每一個字元的色彩。 預設為灰色。

屬性

例外狀況

設定作業中指定的色彩不是 ConsoleColor 的有效成員。

使用者沒有執行這項動作的權限。

發生 I/O 錯誤。

範例

下列範例會檢查主控台的背景色彩是否為黑色,如果是,則會將背景色彩變更為紅色,並將前景色彩變更為黑色。

C#
using System;

public class Example
{
   public static void Main()
   {
      if (Console.BackgroundColor == ConsoleColor.Black) {
         Console.BackgroundColor = ConsoleColor.Red;
         Console.ForegroundColor = ConsoleColor.Black;
         Console.Clear();
      }
   }
}

下列範例會將列舉的值 ConsoleColor 儲存至陣列,並將和屬性的目前值儲存 BackgroundColor ForegroundColor 至變數。 然後,它會將前景色彩變更為列舉中的每個色彩( ConsoleColor 除了符合目前背景的色彩),並將背景色彩變更為列舉中的每個色彩, ConsoleColor 但與目前前景相符的色彩除外。 (如果前景色彩與背景色彩相同,則不會顯示文字。 ) 最後,它會呼叫 ResetColor 方法來還原原始的主控台色彩。

C#
using System;

class Example
{
   public static void Main()
   {
      // Get an array with the values of ConsoleColor enumeration members.
      ConsoleColor[] colors = (ConsoleColor[]) ConsoleColor.GetValues(typeof(ConsoleColor));
      // Save the current background and foreground colors.
      ConsoleColor currentBackground = Console.BackgroundColor;
      ConsoleColor currentForeground = Console.ForegroundColor;

      // Display all foreground colors except the one that matches the background.
      Console.WriteLine("All the foreground colors except {0}, the background color:",
                        currentBackground);
      foreach (var color in colors) {
         if (color == currentBackground) continue;

         Console.ForegroundColor = color;
         Console.WriteLine("   The foreground color is {0}.", color);
      }
      Console.WriteLine();
      // Restore the foreground color.
      Console.ForegroundColor = currentForeground;

      // Display each background color except the one that matches the current foreground color.
      Console.WriteLine("All the background colors except {0}, the foreground color:",
                        currentForeground);
      foreach (var color in colors) {
         if (color == currentForeground) continue;

         Console.BackgroundColor = color;
         Console.WriteLine("   The background color is {0}.", color);
      }

      // Restore the original console colors.
      Console.ResetColor();
      Console.WriteLine("\nOriginal colors restored...");
   }
}
//The example displays output like the following:
//    All the foreground colors except DarkCyan, the background color:
//       The foreground color is Black.
//       The foreground color is DarkBlue.
//       The foreground color is DarkGreen.
//       The foreground color is DarkRed.
//       The foreground color is DarkMagenta.
//       The foreground color is DarkYellow.
//       The foreground color is Gray.
//       The foreground color is DarkGray.
//       The foreground color is Blue.
//       The foreground color is Green.
//       The foreground color is Cyan.
//       The foreground color is Red.
//       The foreground color is Magenta.
//       The foreground color is Yellow.
//       The foreground color is White.
//
//    All the background colors except White, the foreground color:
//       The background color is Black.
//       The background color is DarkBlue.
//       The background color is DarkGreen.
//       The background color is DarkCyan.
//       The background color is DarkRed.
//       The background color is DarkMagenta.
//       The background color is DarkYellow.
//       The background color is Gray.
//       The background color is DarkGray.
//       The background color is Blue.
//       The background color is Green.
//       The background color is Cyan.
//       The background color is Red.
//       The background color is Magenta.
//       The background color is Yellow.
//
//    Original colors restored...

備註

Windows 應用程式的 get 作業,其中主控台不存在,會傳回 Gray 。 Unix 系統不提供任何一般機制來提取目前的主控台色彩。 因此,會傳回 ForegroundColor(ConsoleColor)-1 直到以明確方式設定 (使用 setter) 。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1