使用英语阅读

通过


Console.TreatControlCAsInput 属性

定义

获取或设置一个值,该值指示是将修改键 Control 和控制台键 C 的组合 (Ctrl+C) 视为普通输入,还是视为由操作系统处理的中断。

[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static bool TreatControlCAsInput { get; set; }
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool TreatControlCAsInput { get; set; }
public static bool TreatControlCAsInput { get; set; }

属性值

如果将 Ctrl+C 视为普通输入,则为 true;否则为 false

属性

例外

无法获取或设置控制台输入缓冲区的输入模式。

示例

以下示例演示 了 TreatControlCAsInput 属性。

using System;

class Example
{
   public static void Main()
   {
      ConsoleKeyInfo cki;
      // Prevent example from ending if CTL+C is pressed.
      Console.TreatControlCAsInput = true;

      Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.");
      Console.WriteLine("Press the Escape (Esc) key to quit: \n");
      do
      {
         cki = Console.ReadKey();
         Console.Write(" --- You pressed ");
         if((cki.Modifiers & ConsoleModifiers.Alt) != 0) Console.Write("ALT+");
         if((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write("SHIFT+");
         if((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write("CTL+");
         Console.WriteLine(cki.Key.ToString());
       } while (cki.Key != ConsoleKey.Escape);
    }
}
// This example displays output similar to the following:
//       Press any combination of CTL, ALT, and SHIFT, and a console key.
//       Press the Escape (Esc) key to quit:
//
//       a --- You pressed A
//       k --- You pressed ALT+K
//       ► --- You pressed CTL+P
//         --- You pressed RightArrow
//       R --- You pressed SHIFT+R
//                --- You pressed CTL+I
//       j --- You pressed ALT+J
//       O --- You pressed SHIFT+O
//       § --- You pressed CTL+U

注解

如果 属性falseTreatControlCAsInput值为 并且按 Ctrl+C,则按下的键不会存储在输入缓冲区中,操作系统将终止当前正在执行的进程。 这是默认值。

注意

谨慎使用此属性,因为将其设置为 true 具有如此戏剧性的效果。 大多数用户希望 Ctrl+C 终止控制台应用程序。 如果禁用 Ctrl+C 的效果,用户必须记住使用 Ctrl+Break 终止应用程序,这是一种不太熟悉的组合键。

适用于

产品 版本
.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, 8, 9, 10
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

另请参阅