Export (0) Print
Expand All

FileStream.Flush Method

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

[Visual Basic]
Overrides Public Sub Flush()
[C#]
public override void Flush();
[C++]
public: void Flush();
[JScript]
public override function Flush();

Exceptions

Exception Type Condition
IOException An I/O error occurs.
ObjectDisposedException The stream is closed.

Remarks

This method overrides Flush.

Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close. Setting AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.

Because a buffer can be used for either reading or writing, Flush performs the following two functions:

  1. Any data previously written to the buffer is copied to the file and the buffer is cleared except for its encoder state.
  2. If CanSeek is true and data was previously copied from the file to the buffer for reading, the current position within the file is decremented by the number of unread bytes in the buffer. The buffer is then cleared.

Example

[Visual Basic, C#, C++] This code example is part of a larger example provided for the Lock method.

[Visual Basic] 
' Update the file.
Case "W"C
    Try
        aFileStream.Seek(textLength, _
            SeekOrigin.Begin)
        aFileStream.Read( _
            readText, textLength - 1, byteCount)
        tempString = New String( _
            uniEncoding.GetChars( _
            readText, textLength - 1, byteCount))
        recordNumber = _
            Integer.Parse(tempString) + 1
        aFileStream.Seek( _
            textLength, SeekOrigin.Begin)
        aFileStream.Write(uniEncoding.GetBytes( _
            recordNumber.ToString()), 0, byteCount)
        aFileStream.Flush()
        Console.WriteLine( _
            "Record has been updated.")

[C#] 
// Update the file.
case 'W':
    try
    {
        fileStream.Seek(textLength, 
            SeekOrigin.Begin);
        fileStream.Read(
            readText, textLength - 1, byteCount);
        tempString = new String(
            uniEncoding.GetChars(
            readText, textLength - 1, byteCount));
        recordNumber = int.Parse(tempString) + 1;
        fileStream.Seek(
            textLength, SeekOrigin.Begin);
        fileStream.Write(uniEncoding.GetBytes(
            recordNumber.ToString()), 
            0, byteCount);
        fileStream.Flush();
        Console.WriteLine(
            "Record has been updated.");
    }

[C++] 
// Update the file.
case 'W':
    try
    {
        fileStream->Seek(textLength, 
            SeekOrigin::Begin);
        fileStream->Read(
            readText, textLength - 1, byteCount);
        tempString = new String(
            uniEncoding->GetChars(
            readText, textLength - 1, byteCount));
        recordNumber = Int32::Parse(tempString) + 1;
        fileStream->Seek(
            textLength, SeekOrigin::Begin);
        fileStream->Write(uniEncoding->GetBytes(
            recordNumber.ToString()), 0, byteCount);
        fileStream->Flush();
        Console::WriteLine(
            S"Record has been updated.");
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

FileStream Class | FileStream Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft