System.IO.StreamReader.ReadToEnd Method

Reads all characters from the current position to the end of the stream.

Syntax

public override string ReadToEnd ()

Returns

The rest of the stream as a string, from the current position to the end. If the current position is at the end of the stream, returns an empty string ("").

Exceptions

TypeReason
System.IO.IOExceptionAn I/O error occurred.
OutOfMemoryExceptionThere is insufficient memory to allocate a buffer for the returned string.

Remarks

This method overrides TextReader.ReadToEnd.

StreamReader.ReadToEnd works best when you need to read all the input from the current position to the end of the stream. If more control is needed over how many characters are read from the stream, use the StreamReader.Read(Char[], int, int) method overload, which generally results in better performance.

StreamReader.ReadToEnd assumes that the stream knows when it has reached an end. For interactive protocols in which the server sends data only when you ask for it and does not close the connection, StreamReader.ReadToEnd might block indefinitely because it does not reach an end, and should be avoided.

Note that when using the StreamReader.Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream. If the size of the buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes).

If the current method throws an OutOfMemoryException, the reader's position in the underlying System.IO.Stream object is advanced by the number of characters the method was able to read, but the characters already read into the internal StreamReader.ReadLine buffer are discarded. If you manipulate the position of the underlying stream after reading data into the buffer, the position of the underlying stream might not match the position of the internal buffer. To reset the internal buffer, call the StreamReader.DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary.

For a list of common I/O tasks, see Common I/O Tasks.

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0