📜  C#中的控制台类

📅  最后修改于: 2021-05-29 19:21:43             🧑  作者: Mango

控制台是一个操作系统窗口,用户可以通过该窗口与操作系统进行通信,或者可以说控制台是一个应用程序,在该应用程序中,我们可以将文本作为键盘的输入,并作为文本从计算机端获取。命令提示符是Windows中的控制台的示例,它可以接受MS-DOS命令。控制台包含两个名为屏幕缓冲区的属性和一个控制台窗口。
在C#中,Console类用于表示控制台应用程序的标准输入,输出和错误流。不允许您继承Console类。此类在系统名称空间下定义。此类不包含任何构造函数。此类提供了执行操作的不同类型的属性和方法,而不是构造方法。

特性

Property Description
BackgroundColor Gets or sets the background color of the console.
BufferHeight Gets or sets the height of the buffer area.
BufferWidth Gets or sets the width of the buffer area.
CapsLock Gets a value indicating whether the CAPS LOCK keyboard toggle is turned on or turned off.
CursorLeft Gets or sets the column position of the cursor within the buffer area.
CursorSize Gets or sets the height of the cursor within a character cell.
CursorTop Gets or sets the row position of the cursor within the buffer area.
CursorVisible Gets or sets a value indicating whether the cursor is visible.
Error Gets the standard error output stream.
ForegroundColor Gets or sets the foreground color of the console.
In Gets the standard input stream.
InputEncoding Gets or sets the encoding the console uses to read input.
IsErrorRedirected Gets a value that indicates whether the error output stream has been redirected from the standard error stream.
IsInputRedirected Gets a value that indicates whether input has been redirected from the standard input stream.
IsOutputRedirected Gets a value that indicates whether output has been redirected from the standard output stream.
KeyAvailable Gets a value indicating whether a key press is available in the input stream.
LargestWindowHeight Gets the largest possible number of console window rows, based on the current font and screen resolution.
LargestWindowWidth Gets the largest possible number of console window columns, based on the current font and screen resolution.
NumberLock Gets a value indicating whether the NUM LOCK keyboard toggle is turned on or turned off.
Out Gets the standard output stream.
OutputEncoding Gets or sets the encoding the console uses to write output.
Title Gets or sets the title to display in the console title bar.
TreatControlCAsInput Gets or sets a value indicating whether the combination of the Control modifier key and C console key (Ctrl+C) is treated as ordinary input or as an interruption that is handled by the operating system.
WindowHeight Gets or sets the height of the console window area.
WindowLeft Gets or sets the leftmost position of the console window area relative to the screen buffer.
WindowTop Gets or sets the top position of the console window area relative to the screen buffer.
WindowWidth Gets or sets the width of the console window.

例子:

// C# program to illustrate how to get
// Background and Foreground color
// of the console
using System;
  
public class GFG {
  
    static public void Main()
    {
  
        // Get the Background and foreground 
        // color of Console Using BackgroundColor
        // and ForegroundColor property of Console
        Console.WriteLine("Background color  :{0}",
                        Console.BackgroundColor);
  
        Console.WriteLine("Foreground color : {0}", 
                        Console.ForegroundColor);
    }
}
输出:
Background color : Black
Foreground color : Black

方法

Method Description
Beep() Plays the sound of a beep through the console speaker.
Clear() Clears the console buffer and corresponding console window of display information.
MoveBufferArea() Copies a specified source area of the screen buffer to a specified destination area.
OpenStandardError() Acquires the standard error stream.
OpenStandardInput() Acquires the standard input stream.
OpenStandardOutput() Acquires the standard output stream.
Read() Reads the next character from the standard input stream.
ReadKey() Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.
ReadLine() Reads the next line of characters from the standard input stream.
ResetColor() Sets the foreground and background console colors to their defaults.
SetBufferSize(Int32, Int32) Sets the height and width of the screen buffer area to the specified values.
SetCursorPosition(Int32, Int32) Sets the position of the cursor.
SetError(TextWriter) Sets the Error property to the specified TextWriter object.
SetIn(TextReader) Sets the In property to the specified TextReader object.
SetOut(TextWriter) Sets the Out property to the specified TextWriter object.
SetWindowPosition(Int32, Int32) Sets the position of the console window relative to the screen buffer.
SetWindowSize(Int32, Int32) Sets the height and width of the console window to the specified values.
Write() Writes the text representation of the specified value or values to the standard output stream.
WriteLine() Writes the specified data, followed by the current line terminator, to the standard output stream.

例子:

// C# program to illustrate the concept
// of WriteLine(String) method in console
using System;
  
public class GFG {
  
    static public void Main()
    {
  
        // WriteLine(String) method is used 
        // to display the string
        Console.WriteLine("Welcome to GeeksforGeeks");
        Console.WriteLine("This is a tutorial of Console Class");
    }
}
输出:
Welcome to GeeksforGeeks
This is a tutorial of Console Class

大事记

Event Description
CancelKeyPress Occurs when the Control modifier key (Ctrl) and either the C console key (C) or the Break key are pressed simultaneously (Ctrl+C or Ctrl+Break).

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.console?view=netframework-4.7.2#definition