StringReader.ReadToEnd 方法

定義

讀取從目前位置到字串末端的所有字元,並將它們以單一字串傳回。Reads all characters from the current position to the end of the string and returns them as a single string.

public:
 override System::String ^ ReadToEnd();
public override string ReadToEnd ();
override this.ReadToEnd : unit -> string
Public Overrides Function ReadToEnd () As String

傳回

基礎字串從目前位置至結尾的內容。The content from the current position to the end of the underlying string.

例外狀況

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

目前的讀取器已關閉。The current reader is closed.

範例

這個程式碼範例是針對 TextReader 類別所提供的較大範例的一部分。This code example is part of a larger example provided for the TextReader class.

void ReadText( TextReader^ textReader )
{
   Console::WriteLine( "From {0} - {1}", textReader->GetType()->Name, textReader->ReadToEnd() );
}


static void ReadText(TextReader textReader)
{
    Console.WriteLine("From {0} - {1}", 
        textReader.GetType().Name, textReader.ReadToEnd());
}
Shared Sub ReadText(aTextReader As TextReader)
    Console.WriteLine("From {0} - {1}", _
        aTextReader.GetType().Name, aTextReader.ReadToEnd())
End Sub

備註

這個方法會覆寫 TextReader.ReadToEnd 方法。This method overrides the TextReader.ReadToEnd method.

如果目前的方法擲回 OutOfMemoryException,則讀取器在基礎字串中的位置會由方法可讀取的字元數來增加,但是已經讀入內部 ReadToEnd 緩衝區的字元會被捨棄。If the current method throws an OutOfMemoryException, the reader's position in the underlying string is advanced by the number of characters the method was able to read, but the characters already read into the internal ReadToEnd buffer are discarded. 因為無法變更讀取器在字串中的位置,所以已讀取的字元無法復原,而且只能藉由重新初始化 StringReader來存取。Because the position of the reader in the string cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the StringReader. 若要避免這種情況,請使用 Read 方法,並將讀取的字元儲存在預先配置的緩衝區中。To avoid such a situation, use the Read method and store the read characters in a preallocated buffer.

下表列出其他一般或相關 i/o 工作的範例。The following table lists examples of other typical or related I/O tasks.

若要執行相關作業…To do this... 請參閱這個主題中的範例…See the example in this topic...
建立文字檔Create a text file. 如何:將文字寫入檔案How to: Write Text to a File
寫入文字檔。Write to a text file. 如何:將文字寫入檔案How to: Write Text to a File
從文字檔讀取。Read from a text file. 如何:從檔案讀取文字How to: Read Text from a File
將文字附加至檔案。Append text to a file. 如何:開啟並附加至記錄檔How to: Open and Append to a Log File

File.AppendText

FileInfo.AppendText
取得檔案的大小。Get the size of a file. FileInfo.Length
取得檔案的屬性。Get the attributes of a file. File.GetAttributes
設定檔案的屬性。Set the attributes of a file. File.SetAttributes
判斷檔案是否存在。Determine if a file exists. File.Exists
讀取二進位檔案。Read from a binary file. 如何:讀取和寫入新建立的資料檔案How to: Read and Write to a Newly Created Data File
寫入二進位檔案。Write to a binary file. 如何:讀取和寫入新建立的資料檔案How to: Read and Write to a Newly Created Data File

適用於

另請參閱