在 .NET Framework 中建置主控台應用程式Building Console Applications in the .NET Framework

.NET Framework 中的應用程式可以使用 System.Console 類別從主控台讀取字元,以及將字元寫入主控台。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. 在應用程式啟動時,這些資料流會自動與主控台產生關聯,並且分別表示為 InOutError 屬性。These streams are automatically associated with the console when the application starts and are presented as the In, Out, and Error properties, respectively.

Console.In 屬性的值是一個 System.IO.TextReader 物件,而 Console.OutConsole.Error 屬性的值則為 System.IO.TextWriter 物件。The value of the Console.In property is a System.IO.TextReader object, whereas the values of the Console.Out and Console.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. 例如,您可以將 Console.Out 屬性設定為 System.IO.StreamWriter,這樣會透過 Console.SetOut 方法封裝 System.IO.FileStream,藉此將輸出重新導向至檔案。For example, you can redirect the output to a file by setting the Console.Out property to a System.IO.StreamWriter, which encapsulates a System.IO.FileStream by means of the Console.SetOut method. Console.InConsole.Out 屬性不需要參考相同資料流。The Console.In and Console.Out properties do not need to refer to the same stream.

注意

如需建置主控台應用程式的詳細資訊 (含 C#、Visual Basic 及 C++ 的範例),請參閱 Console 類別的文件。For more information about building console applications, including examples in C#, Visual Basic, and C++, see the documentation for the Console class.

因為沒有可以將資訊寫入的主控台,所以如果主控台不存在 (例如在 Windows 架構應用程式中) 的話,將看不到寫入標準輸出資料流的輸出。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. 將資訊寫入不可存取的主控台不會導致引發例外狀況。Writing information to an inaccessible console does not cause an exception to be raised.

此外,若要在使用 Visual Studio 開發的 Windows 架構應用程式內啟用主控台來讀取和寫入,請開啟專案的 [屬性]**** 對話方塊,按一下 [應用程式]**** 索引標籤,然後將 [應用程式類型]**** 設定為 [主控台應用程式]****。Alternately, to enable the console for reading and writing within a Windows-based application that is developed using Visual Studio, open the project's Properties dialog box, click the Application tab, and set the Application type to Console Application.

主控台應用程式缺乏預設會啟動的訊息幫浦 (Message Pump)。Console applications lack a message pump that starts by default. 因此,對 Microsoft Win32 計時器的主控台呼叫可能會失敗。Therefore, console calls to Microsoft Win32 timers might fail.

System.Console 類別具有可以從主控台讀取個別字元或整行的方法。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 Types.

另請參閱See also