Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

BinaryReader Constructor

.NET Framework 1.1

Initializes a new instance of the BinaryReader class.

Overload List

Initializes a new instance of the BinaryReader class based on the supplied stream and using UTF8Encoding.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Stream)
[C#] public BinaryReader(Stream);
[C++] public: BinaryReader(Stream*);
[JScript] public function BinaryReader(Stream);

Initializes a new instance of the BinaryReader class based on the supplied stream and a specific character encoding.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Stream, Encoding)
[C#] public BinaryReader(Stream, Encoding);
[C++] public: BinaryReader(Stream*, Encoding*);
[JScript] public function BinaryReader(Stream, Encoding);

Example

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

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the BinaryReader constructor. For other examples that might be available, see the individual overload topics.
[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.

See Also

BinaryReader Class | BinaryReader Members | System.IO Namespace

Show:
© 2015 Microsoft