Export (0) Print
Expand All

StreamReader.ReadToEnd Method

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

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public override string ReadToEnd ()
public String ReadToEnd ()
public override function ReadToEnd () : String
Not applicable.

Return Value

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 the empty string("").

Exception typeCondition

OutOfMemoryException

There is insufficient memory to allocate a buffer for the returned string.

IOException

An I/O error occurs.

This method overrides TextReader.ReadToEnd.

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 Read(Char[],Int32,Int32) method overload, which generally results in better performance.

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, ReadToEnd might block indefinitely and should be avoided.

Note than when using the 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 Stream object is advanced by the number of characters the method was able to read, but the characters already read into the internal ReadLine buffer are discarded. Since the position of the reader in the stream cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the StreamReader object. If the initial position within the stream is unknown or the stream does not support seeking, the underlying Stream object also needs to be reinitialized.

To avoid such a situation and produce robust code you should use the Read method and store the read characters in a pre-allocated buffer.

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

The following code example reads all the way to the end of a file in one operation.

using System;
using System.IO;

class Test 
{
	
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";

        try 
        {
            if (File.Exists(path)) 
            {
                File.Delete(path);
            }

            using (StreamWriter sw = new StreamWriter(path)) 
            {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("Reading");
            }

            using (StreamReader sr = new StreamReader(path)) 
            {
                //This allows you to do one Read operation.
                Console.WriteLine(sr.ReadToEnd());
            }
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        String path = "c:\\temp\\MyTest.txt";

        try {
            if (File.Exists(path)) {
                File.Delete(path);
            }
            StreamWriter sw = new StreamWriter(path);
            try {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("Reading");
            }
            finally {
                sw.Dispose();
            }
            StreamReader sr = new StreamReader(path);
            try {
                //This allows you to do one Read operation.
                Console.WriteLine(sr.ReadToEnd());
            }
            finally {
                sr.Dispose();
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    } //main
} //Test

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft