Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

BinaryReader.Close Method

Closes the current reader and the underlying stream.

[Visual Basic]
Public Overridable Sub Close()
[C#]
public virtual void Close();
[C++]
public: virtual void Close();
[JScript]
public function Close();

Remarks

This implementation of Close calls the Dispose method passing a true value.

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.

Example

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

[Visual Basic] 
Dim binReader As New BinaryReader( _
    File.Open(fileName, FileMode.Open))
Try

    ' If the file is not empty, 
    ' read the application settings.
    If binReader.PeekChar() <> -1 Then
        aspRatio   = binReader.ReadSingle()
        lkupDir     = binReader.ReadString()
        saveTime  = binReader.ReadInt32()
        statusBar = binReader.ReadBoolean()
        Return
    End If

' If the end of the stream is reached before reading
' the four data values, ignore the error and use the
' default settings for the remaining values.
Catch ex As EndOfStreamException
    Console.WriteLine("{0} caught and ignored. " & _ 
        "Using default values.", ex.GetType().Name)
Finally
    binReader.Close()
End Try

[C#] 
BinaryReader binReader = 
    new BinaryReader(File.Open(fileName, FileMode.Open));
try
{
    // If the file is not empty, 
    // read the application settings.
    if(binReader.PeekChar() != -1)
    {
        aspectRatio   = binReader.ReadSingle();
        lookupDir     = binReader.ReadString();
        autoSaveTime  = binReader.ReadInt32();
        showStatusBar = binReader.ReadBoolean();
    }
}

// If the end of the stream is reached before reading
// the four data values, ignore the error and use the
// default settings for the remaining values.
catch(EndOfStreamException e)
{
    Console.WriteLine("{0} caught and ignored. " + 
        "Using default values.", e.GetType().Name);
}
finally
{
    binReader.Close();
}

[C++] 
BinaryReader* binReader = 
    new BinaryReader(File::Open(fileName, FileMode::Open));
try
{
    // If the file is not empty, 
    // read the application settings.
    if(binReader->PeekChar() != -1)
    {
        aspectRatio   = binReader->ReadSingle();
        lookupDir     = binReader->ReadString();
        autoSaveTime  = binReader->ReadInt32();
        showStatusBar = binReader->ReadBoolean();
        return;
    }
}

// If the end of the stream is reached before reading
// the four data values, ignore the error and use the
// default settings for the remaining values.
catch(EndOfStreamException* e)
{
    Console::WriteLine(S"{0} caught and ignored. "
        S"Using default values.", e->GetType()->Name);
}
__finally
{
    binReader->Close();
}

[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

See Also

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

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