Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

StreamReader.DiscardBufferedData Method

Clears the internal buffer.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)
public void DiscardBufferedData()

Use the DiscardBufferedData method to reset the internal buffer for the StreamReader object. You need to call this method only when the position of the internal buffer and the BaseStream do not match. These positions can become mismatched when you read data into the buffer and then seek a new position in the underlying stream. This method slows performance and should be used only when absolutely necessary, such as when you want to read a portion of the contents of a StreamReader object more than once.

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

The following example shows a scenario where the DiscardBufferedData method must be called to synchronize the internal buffer and the underlying stream. The file in the example is used to illustrate position and consists of the text abcdefghijklmnopqrstuvwxyz. By calling DiscardBufferedData after the data is read, the example works as expected. After the first 15 characters are read, the position is reset to the offset value of 2 and all the remaining characters are read. If you remove the call to DiscardBufferedData, the example does not work as expected. The first 15 characters are read, but only the position of the underlying stream is reset. The internal buffer of the StreamReader object is still on the 16th character. Therefore, ReadToEnd returns all the characters in the buffer plus the characters in the underlying stream starting from the reset position.

using System;
using System.IO;

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

        using (StreamReader sr = new StreamReader(path))
        {
            char[] c = null;

            c = new char[15];
            sr.Read(c, 0, c.Length);
            Console.WriteLine("first 15 characters:");
            Console.WriteLine(c);
            // writes - "abcdefghijklmno"

            sr.DiscardBufferedData();
            sr.BaseStream.Seek(2, SeekOrigin.Begin);
            Console.WriteLine("\nBack to offset 2 and read to end: ");
            Console.WriteLine(sr.ReadToEnd());
            // writes - "cdefghijklmnopqrstuvwxyz" 
            // without DiscardBufferedData, writes - "pqrstuvwxyzcdefghijklmnopqrstuvwxyz"
        }
    }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.