This documentation is archived and is not being maintained.

BufferedStream.Flush Method

Clears all buffers for this stream and causes any buffered data to be written to the underlying device.

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

public override void Flush ()
public void Flush ()
public override function Flush ()
Not applicable.

Exception typeCondition

IOException

The data source or repository is not open.

For an example of creating a file and writing text to a file, see Writing Text to a File. For an example of reading text from a file, see Reading Text from a File. For an example of reading from and writing to a binary file, see Reading and Writing to a Newly Created Data File.

Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close.

If you use the BufferedStream constructor, thus specifying the buffer size while creating the BufferedStream object, the content is flushed when it reaches the buffer size. For example, code such as BufferedStream bs = new BufferedStream(bs, 5) will flush the content when the buffer size reaches 5 bytes.

All the read and write methods of BufferedStream automatically maintain the buffer, so there is no need to invoke Flush when switching back and forth between reading and writing.

This code example is part of a larger example provided for the BufferedStream class.

// Send the data using the BufferedStream.
Console.WriteLine("Sending data using BufferedStream.");
startTime = DateTime.Now;
for(int i = 0; i < numberOfLoops; i++)
{
    bufStream.Write(dataToSend, 0, dataToSend.Length);
}
bufStream.Flush();
bufferedTime = (DateTime.Now - startTime).TotalSeconds;
Console.WriteLine("{0} bytes sent in {1} seconds.\n",
    numberOfLoops * dataToSend.Length, 
    bufferedTime.ToString("F1"));

// Send the data using the BufferedStream.
Console.WriteLine("Sending data using BufferedStream.");
startTime = DateTime.get_Now();
for(int i=0;i < numberOfLoops;i++) {
    bufStream.Write(dataToSend, 0, dataToSend.length);
}        
bufStream.Flush();
bufferedTime = 
    ((DateTime.get_Now()).Subtract(startTime)).get_TotalSeconds();
Console.WriteLine("{0} bytes sent in {1} seconds.\n",
    System.Convert.ToString (numberOfLoops * dataToSend.length),
    ((System.Double)bufferedTime).ToString("F1"));

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, 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
Show: