Export (0) Print
Expand All

Console Class

Updated: May 2012

Represents the standard input, output, and error streams for console applications. This class cannot be inherited.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public NotInheritable Class Console

The Console type exposes the following members.

  NameDescription
Public propertyStatic memberBackgroundColorGets or sets the background color of the console.
Public propertyStatic memberBufferHeightGets or sets the height of the buffer area.
Public propertyStatic memberBufferWidthGets or sets the width of the buffer area.
Public propertyStatic memberCapsLockGets a value indicating whether the CAPS LOCK keyboard toggle is turned on or turned off.
Public propertyStatic memberCursorLeftGets or sets the column position of the cursor within the buffer area.
Public propertyStatic memberCursorSizeGets or sets the height of the cursor within a character cell.
Public propertyStatic memberCursorTopGets or sets the row position of the cursor within the buffer area.
Public propertyStatic memberCursorVisibleGets or sets a value indicating whether the cursor is visible.
Public propertyStatic memberSupported by the XNA FrameworkErrorGets the standard error output stream.
Public propertyStatic memberForegroundColorGets or sets the foreground color of the console.
Public propertyStatic memberSupported by the XNA FrameworkInGets the standard input stream.
Public propertyStatic memberInputEncodingGets or sets the encoding the console uses to read input.
Public propertyStatic memberKeyAvailableGets a value indicating whether a key press is available in the input stream.
Public propertyStatic memberLargestWindowHeightGets the largest possible number of console window rows, based on the current font and screen resolution.
Public propertyStatic memberLargestWindowWidthGets the largest possible number of console window columns, based on the current font and screen resolution.
Public propertyStatic memberNumberLockGets a value indicating whether the NUM LOCK keyboard toggle is turned on or turned off.
Public propertyStatic memberSupported by the XNA FrameworkOutGets the standard output stream.
Public propertyStatic memberOutputEncodingGets or sets the encoding the console uses to write output.
Public propertyStatic memberTitleGets or sets the title to display in the console title bar.
Public propertyStatic memberTreatControlCAsInputGets 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.
Public propertyStatic memberWindowHeightGets or sets the height of the console window area.
Public propertyStatic memberWindowLeftGets or sets the leftmost position of the console window area relative to the screen buffer.
Public propertyStatic memberWindowTopGets or sets the top position of the console window area relative to the screen buffer.
Public propertyStatic memberWindowWidthGets or sets the width of the console window.
Top

  NameDescription
Public methodStatic memberBeepPlays the sound of a beep through the console speaker.
Public methodStatic memberBeep(Int32, Int32)Plays the sound of a beep of a specified frequency and duration through the console speaker.
Public methodStatic memberClearClears the console buffer and corresponding console window of display information.
Public methodStatic memberMoveBufferArea(Int32, Int32, Int32, Int32, Int32, Int32)Copies a specified source area of the screen buffer to a specified destination area.
Public methodStatic memberMoveBufferArea(Int32, Int32, Int32, Int32, Int32, Int32, Char, ConsoleColor, ConsoleColor)Copies a specified source area of the screen buffer to a specified destination area.
Public methodStatic memberOpenStandardErrorAcquires the standard error stream.
Public methodStatic memberOpenStandardError(Int32)Acquires the standard error stream, which is set to a specified buffer size.
Public methodStatic memberOpenStandardInputAcquires the standard input stream.
Public methodStatic memberOpenStandardInput(Int32)Acquires the standard input stream, which is set to a specified buffer size.
Public methodStatic memberOpenStandardOutputAcquires the standard output stream.
Public methodStatic memberOpenStandardOutput(Int32)Acquires the standard output stream, which is set to a specified buffer size.
Public methodStatic memberReadReads the next character from the standard input stream.
Public methodStatic memberReadKeyObtains the next character or function key pressed by the user. The pressed key is displayed in the console window.
Public methodStatic memberReadKey(Boolean)Obtains the next character or function key pressed by the user. The pressed key is optionally displayed in the console window.
Public methodStatic memberSupported by the XNA FrameworkReadLineReads the next line of characters from the standard input stream.
Public methodStatic memberResetColorSets the foreground and background console colors to their defaults.
Public methodStatic memberSetBufferSizeSets the height and width of the screen buffer area to the specified values.
Public methodStatic memberSetCursorPositionSets the position of the cursor.
Public methodStatic memberSupported by the XNA FrameworkSetErrorSets the Error property to the specified TextWriter object.
Public methodStatic memberSupported by the XNA FrameworkSetInSets the In property to the specified TextReader object.
Public methodStatic memberSupported by the XNA FrameworkSetOutSets the Out property to the specified TextWriter object.
Public methodStatic memberSetWindowPositionSets the position of the console window relative to the screen buffer.
Public methodStatic memberSetWindowSizeSets the height and width of the console window to the specified values.
Public methodStatic memberWrite(Boolean)Writes the text representation of the specified Boolean value to the standard output stream.
Public methodStatic memberSupported by the XNA FrameworkWrite(Char)Writes the specified Unicode character value to the standard output stream.
Public methodStatic memberSupported by the XNA FrameworkWrite(Char())Writes the specified array of Unicode characters to the standard output stream.
Public methodStatic memberWrite(Decimal)Writes the text representation of the specified Decimal value to the standard output stream.
Public methodStatic memberWrite(Double)Writes the text representation of the specified double-precision floating-point value to the standard output stream.
Public methodStatic memberSupported by the XNA FrameworkWrite(Int32)Writes the text representation of the specified 32-bit signed integer value to the standard output stream.
Public methodStatic memberWrite(Int64)Writes the text representation of the specified 64-bit signed integer value to the standard output stream.
Public methodStatic memberSupported by the XNA FrameworkWrite(Object)Writes the text representation of the specified object to the standard output stream.
Public methodStatic memberWrite(Single)Writes the text representation of the specified single-precision floating-point value to the standard output stream.
Public methodStatic memberSupported by the XNA FrameworkWrite(String)Writes the specified string value to the standard output stream.
Public methodStatic memberWrite(UInt32)Writes the text representation of the specified 32-bit unsigned integer value to the standard output stream.
Public methodStatic memberWrite(UInt64)Writes the text representation of the specified 64-bit unsigned integer value to the standard output stream.
Public methodStatic memberSupported by the XNA FrameworkWrite(String, Object)Writes the text representation of the specified object to the standard output stream using the specified format information.
Public methodStatic memberSupported by the XNA FrameworkWrite(String, Object())Writes the text representation of the specified array of objects to the standard output stream using the specified format information.
Public methodStatic memberSupported by the XNA FrameworkWrite(Char(), Int32, Int32)Writes the specified subarray of Unicode characters to the standard output stream.
Public methodStatic memberSupported by the XNA FrameworkWrite(String, Object, Object)Writes the text representation of the specified objects to the standard output stream using the specified format information.
Public methodStatic memberSupported by the XNA FrameworkWrite(String, Object, Object, Object)Writes the text representation of the specified objects to the standard output stream using the specified format information.
Public methodStatic memberWrite(String, Object, Object, Object, Object)Writes the text representation of the specified objects and variable-length parameter list to the standard output stream using the specified format information.
Public methodStatic memberSupported by the XNA FrameworkWriteLineWrites the current line terminator to the standard output stream.
Public methodStatic memberWriteLine(Boolean)Writes the text representation of the specified Boolean value, followed by the current line terminator, to the standard output stream.
Public methodStatic memberSupported by the XNA FrameworkWriteLine(Char)Writes the specified Unicode character, followed by the current line terminator, value to the standard output stream.
Public methodStatic memberSupported by the XNA FrameworkWriteLine(Char())Writes the specified array of Unicode characters, followed by the current line terminator, to the standard output stream.
Public methodStatic memberWriteLine(Decimal)Writes the text representation of the specified Decimal value, followed by the current line terminator, to the standard output stream.
Public methodStatic memberWriteLine(Double)Writes the text representation of the specified double-precision floating-point value, followed by the current line terminator, to the standard output stream.
Public methodStatic memberSupported by the XNA FrameworkWriteLine(Int32)Writes the text representation of the specified 32-bit signed integer value, followed by the current line terminator, to the standard output stream.
Public methodStatic memberWriteLine(Int64)Writes the text representation of the specified 64-bit signed integer value, followed by the current line terminator, to the standard output stream.
Public methodStatic memberSupported by the XNA FrameworkWriteLine(Object)Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream.
Public methodStatic memberWriteLine(Single)Writes the text representation of the specified single-precision floating-point value, followed by the current line terminator, to the standard output stream.
Public methodStatic memberSupported by the XNA FrameworkWriteLine(String)Writes the specified string value, followed by the current line terminator, to the standard output stream.
Public methodStatic memberWriteLine(UInt32)Writes the text representation of the specified 32-bit unsigned integer value, followed by the current line terminator, to the standard output stream.
Public methodStatic memberWriteLine(UInt64)Writes the text representation of the specified 64-bit unsigned integer value, followed by the current line terminator, to the standard output stream.
Public methodStatic memberSupported by the XNA FrameworkWriteLine(String, Object)Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream using the specified format information.
Public methodStatic memberSupported by the XNA FrameworkWriteLine(String, Object())Writes the text representation of the specified array of objects, followed by the current line terminator, to the standard output stream using the specified format information.
Public methodStatic memberWriteLine(Char(), Int32, Int32)Writes the specified subarray of Unicode characters, followed by the current line terminator, to the standard output stream.
Public methodStatic memberSupported by the XNA FrameworkWriteLine(String, Object, Object)Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.
Public methodStatic memberSupported by the XNA FrameworkWriteLine(String, Object, Object, Object)Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.
Public methodStatic memberWriteLine(String, Object, Object, Object, Object)Writes the text representation of the specified objects and variable-length parameter list, followed by the current line terminator, to the standard output stream using the specified format information.
Top

  NameDescription
Public eventStatic memberCancelKeyPressOccurs when the Control modifier key (CTRL) and C console key (C) are pressed simultaneously (CTRL+C).
Top

The console is an operating system window where users interact with the operating system or a text-based console application by entering text input through the computer keyboard, and reading text output from the computer terminal. For example, in Windows the console is called the command prompt window and accepts MS-DOS commands. The Console class provides basic support for applications that read characters from, and write characters to, the console.

For information about developing with the Console class, see the following sections:

Console I/O Streams

When a console application starts, the operating system automatically associates three I/O streams with the console. Your application can read user input from the standard input stream; write normal data to the standard output stream; and write error data to the standard error output stream. These streams are presented to your application as the values of the In, Out, and Error properties.

By default, the value of the In property is a System.IO.TextReader object, and the values of the Out and Error properties are System.IO.TextWriter objects. However, you can set these properties to streams that do not represent the console; for example, you can set these properties to streams that represent files. To redirect the standard input, standard output, or standard error stream, call the SetIn, SetOut, or SetError method, respectively. I/O operations using these streams are synchronized, which means multiple threads can read from, or write to, the streams.

NoteNote

You should not use the Console class to display output in unattended applications, such as server applications. Similarly, calls to methods such as Write and WriteLine have no effect in Windows applications.

Console class members that work normally when the underlying stream is directed to a console might throw an exception if the stream is redirected, for example, to a file. Consequently, program your application to catch System.IO.IOException if you redirect a standard stream.

It is sometimes useful to explicitly invoke the members of the stream objects represented by the In, Out, and Error properties. For example, by default, the Console.ReadLine method reads input from the standard input stream. Similarly, the Console.WriteLine method writes data to the standard output stream followed by the default line termination string; that is, data is followed by a carriage return and line feed ("\r\n"). However, the Console class does not provide a corresponding method to write data to the standard error output stream, or a property to change the line termination string for data written to that stream.

You can solve this problem by setting the TextWriter.NewLine property of the Out or Error property to another line termination string. For example, the C# statement, Console.Error.NewLine = "\r\n\r\n";, sets the line termination string for the standard error output stream to two carriage return and line feed sequences. Then you can explicitly call the WriteLine method of the error output stream object, as in the C# statement, Console.Error.WriteLine();.

Screen Buffer and Console Window

Two closely related features of the console are the screen buffer and the console window. Text is actually read from or written to streams owned by the console, but appear to be read from or written to an area owned by the console called the screen buffer. The screen buffer is an attribute of the console, and is organized as a rectangular grid of rows and columns where each grid intersection, or character cell, can contain a character. Each character has its own foreground color and each character cell has its own background color.

The screen buffer is viewed through a rectangular region called the console window. The console window is another attribute of the console; it is not the console itself, which is an operating system window. The console window is arranged in rows and columns, is less than or equal to the size of the screen buffer, and can be moved to view different areas of the underlying screen buffer. If the screen buffer is larger than the console window, the console automatically displays scroll bars so the console window can be repositioned over the screen buffer area.

A cursor indicates the screen buffer position where text is currently read or written. The cursor can be hidden or made visible, and its height can be changed. If the cursor is visible, the console window position is moved automatically so the cursor is always in view.

The origin for character cell coordinates in the screen buffer is the upper left corner, and the position of the cursor and the console window are measured relative to that origin. Use zero-based indexes to specify positions; that is, specify the topmost row as row 0, and the leftmost column as column 0. The maximum value for the row and column indexes is Int16.MaxValue.

Common Operations

The Console class contains the following methods for reading console input and writing console output:

  • The overloads of the ReadKey method read an individual character.

  • The ReadLine method reads an entire line of input.

  • The Write method overloads convert an instance of a value type, an array of characters, or a set of objects to a formatted or unformatted string, and then write that string to the console.

  • A parallel set of WriteLine method overloads output the same string as the Write overloads but also add a line termination string.

The Console class also contains methods and properties to perform the following operations:

  • Get or set the size of the screen buffer. The BufferHeight and BufferWidth properties let you get or set the buffer height and width, respectively, and the SetBufferSize method lets you set the buffer size in a single method call.

  • Get or set the size of the console window. The WindowHeight and WindowWidth properties let you get or set the window height and width, respectively, and the SetWindowSize method lets you set the window size in a single method call.

  • Get or set the size of the cursor. The CursorSize property specifies the height of the cursor in a character cell.

  • Get or set the position of the console window relative to the screen buffer. The WindowTop and WindowLeft properties let you get or set the top row and leftmost column of the screen buffer that appears in the console window, and the SetWindowPosition method lets you set these values in a single method call.

  • Get or set the position of the cursor by getting or setting the CursorTop and CursorLeft properties, or set the position of the cursor by calling the SetCursorPosition method.

  • Move or clear data in the screen buffer by calling the MoveBufferArea or Clear method.

  • Get or set the foreground and background colors by using the ForegroundColor and BackgroundColor properties, or reset the background and foreground to their default colors by calling the ResetColor method.

  • Play the sound of a beep through the console speaker by calling the Beep method.

The following example demonstrates how to read data from, and write data to, the standard input and output streams. Note that these streams can be redirected by using the SetIn and SetOut methods.


Public Class Example
    Public Shared Sub Main()
        Console.Write("Hello ")
        Console.WriteLine("World!")
        Console.Write("Enter your name: ")
        Dim name As String = Console.ReadLine()
        Console.Write("Good day, ")
        Console.Write(name)
        Console.WriteLine("!")
    End Sub
End Class 
' The example displays output similar to the following:
'        Hello World!
'        Enter your name: James
'        Good day, James!


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

This type is thread safe.

Date

History

Reason

May 2012

Expanded the Common Operations section.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft