TextReader.ReadLine TextReader.ReadLine TextReader.ReadLine TextReader.ReadLine Method

定義

自文字讀取器讀取一行字元,並將資料以字串傳回。Reads a line of characters from the text reader and returns the data as a string.

public:
 virtual System::String ^ ReadLine();
public virtual string ReadLine ();
abstract member ReadLine : unit -> string
override this.ReadLine : unit -> string
Public Overridable Function ReadLine () As String

傳回

讀取器的下一行,或者如果所有字元都被讀取,則為 nullThe next line from the reader, or null if all characters have been read.

例外狀況

發生 I/O 錯誤。An I/O error occurs.

沒有足夠記憶體可為傳回的字串配置緩衝區。There is insufficient memory to allocate a buffer for the returned string.

下一行中的字元數大於The number of characters in the next line is larger than MaxValue

備註

一條線定義為一串字元後面接著歸位字元 (0x000d)、 換行字元 (0x000a),以歸位字元後面接著換行字元, Environment.NewLine,或資料流結尾標記。A line is defined as a sequence of characters followed by a carriage return (0x000d), a line feed (0x000a), a carriage return followed by a line feed, Environment.NewLine, or the end-of-stream marker. 終止歸位字元或換行字元,並不會包含所傳回的字串。The string that is returned does not contain the terminating carriage return or line feed. 傳回值是null如果已經到達輸入資料流的結尾。The return value is null if the end of the input stream has been reached.

如果方法會擲回OutOfMemoryException例外狀況,在基礎的讀取器的位置Stream進階的方法無法讀取的字元,但已讀入內部的字元數ReadLine緩衝區是捨棄。If the method throws an OutOfMemoryException exception, the reader's position in the underlying Stream is advanced by the number of characters the method was able to read, but the characters that were already read into the internal ReadLine buffer are discarded. 無法變更資料流中讀取器的位置,因為已經讀取的字元是無法復原,並可重新初始化來只存取TextReader物件。Because the position of the reader in the stream cannot be changed, the characters that were already read are unrecoverable and can be accessed only by reinitializing the TextReader object. 如果資料流中的初始位置為未知或資料流不支援搜尋,基礎Stream也需要重新初始化。If the initial position within the stream is unknown or the stream does not support seeking, the underlying Stream also needs to be reinitialized.

若要避免這種情況,並產生強固的程式碼,您應該使用Read讀取方法,並存放區中預先配置的緩衝區的字元。To avoid such a situation and produce robust code you should use the Read method and store the read characters in a preallocated buffer.

TextReader 類別是抽象類別。The TextReader class is an abstract class. 因此,您的程式碼中不必將它執行個體化。Therefore, you do not instantiate it in your code. 如需使用的範例ReadLine方法,請參閱StreamReader.ReadLine方法。For an example of using the ReadLine method, see the StreamReader.ReadLine method.

適用於

另請參閱