TextReader.ReadToEnd TextReader.ReadToEnd TextReader.ReadToEnd TextReader.ReadToEnd Method

定义

读取从当前位置到文本读取器末尾的所有字符并将它们作为一个字符串返回。Reads all characters from the current position to the end of the text reader and returns them as one string.

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

返回

一个包含从当前位置到文本读取器末尾的所有字符的字符串。A string that contains all characters from the current position to the end of the text reader.

异常

出现 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

注解

如果该方法将引发OutOfMemoryException异常,在基础读取器的位置Stream高级的字符的方法是可以读取,但到内部已读取的字符数ReadToEnd缓冲区是被丢弃。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 ReadToEnd buffer are discarded. 无法更改流中读取器的位置,因为已读取的字符是不可恢复,并且可访问只通过重新初始化TextReaderBecause 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. 如果流中的初始位置未知或该流不支持查找,基础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. 有关使用的示例ReadToEnd方法,请参阅StreamReader.ReadToEnd方法。For an example of using the ReadToEnd method, see the StreamReader.ReadToEnd method.

适用于

另请参阅