Freigeben über


Building Console Applications 

Applications in the .NET Framework can use the System.Console class to read characters from and write characters to the console. Data from the console is read from the standard input stream, data to the console is written to the standard output stream, and error data to the console is written to the standard error output stream. These streams are automatically associated with the console when the application starts and are presented as the In, Out, and Error properties, respectively.

The value of the In property is a System.IO.TextReader object, whereas the values of the Out and Error properties are System.IO.TextWriter objects. You can associate these properties with streams that do not represent the console, making it possible for you to point the stream to a different location for input or output. For example, you can redirect the output to a file by setting the Out property to a System.IO.StreamWriter, which encapsulates a FileStream by means of the Console.SetOut method. The Console.In and Console.Out properties do not need to refer to the same stream.

If the console does not exist, as in a Windows-based application, output written to the standard output stream will not be visible, because there is no console to write the information to.

NoteNote

Writing information to an inaccessible console does not cause an exception to be raised.

Alternately, to enable the console for reading and writing within a Windows-based application running under Visual Studio, set the project's Common Properties/General/Output Type to Console Application.

Console applications lack a message pump that starts by default. Therefore, console calls to Microsoft Win32 timers might fail.

The System.Console class has methods that can read individual characters or entire lines from the console. Other methods convert data and format strings, and then write the formatted strings to the console. For more information on formatting strings, see Formatting Overview.

See Also

Reference

System.Console

Concepts

Formatting Overview