Export (0) Print
Expand All

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()

ExceptionCondition
ObjectDisposedException

The stream has been disposed.

IOException

The data source or repository is not open.

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"));

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Show:
© 2014 Microsoft