Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

BinaryReader.BaseStream Property

Exposes access to the underlying stream of the BinaryReader.

[Visual Basic]
Public Overridable ReadOnly Property BaseStream As Stream
[C#]
public virtual Stream BaseStream {get;}
[C++]
public: __property virtual Stream* get_BaseStream();
[JScript]
public function get BaseStream() : Stream;

Property Value

The underlying stream associated with the BinaryReader.

Remarks

CAUTION   Using the underlying stream while reading or while using the BinaryReader can cause data loss and corruption. For example, the same bytes might be read more than once, bytes might be skipped, or character reading might become unpredictable.

The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File

Example

[Visual Basic, C#, C++] The following code example shows how to read and write Double data to memory by using the BinaryReader and BinaryWriter classes on top of the MemoryStream class. MemoryStream only reads and writes Byte data.

[Visual Basic] 
Imports System
Imports System.IO

Public Class BinaryRW

    Shared Sub Main()
    
        Dim i As Integer
        Const upperBound As Integer = 1000

        ' Create random data to write to the stream.
        Dim dataArray(upperBound) As Double
        Dim randomGenerator As New Random()
        For i = 0 To upperBound
            dataArray(i) = 100.1 * randomGenerator.NextDouble()
        Next i

        Dim binWriter As New BinaryWriter(New MemoryStream())
        Try

            ' Write data to the stream.
            Console.WriteLine("Writing data to the stream.")
            
            For i = 0 To upperBound
                binWriter.Write(dataArray(i))
            Next i

            ' Create a reader using the stream from the writer.
            Dim binReader As New BinaryReader(binWriter.BaseStream)

            ' Return to the beginning of the stream.
            binReader.BaseStream.Position = 0

            ' Read and verify the data.
            Try
                Console.WriteLine("Verifying the written data.")
                For i = 0 To upperBound
                    If binReader.ReadDouble() <> dataArray(i) Then
                        Console.WriteLine("Error writing data.")
                        Exit For
                    End If
                Next i
                Console.WriteLine("The data was written and verified.")
            Catch ex As EndOfStreamException
                Console.WriteLine("Error writing data: {0}.", _
                    ex.GetType().Name)
            End Try
        Finally
            binWriter.Close()
        End Try

    End Sub
End Class

[C#] 
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        int i;
        const int arrayLength = 1000;

        // Create random data to write to the stream.
        Random randomGenerator = new Random();
        double[] dataArray = new double[arrayLength];
        for(i = 0; i < arrayLength; i++)
        {
            dataArray[i] = 100.1 * randomGenerator.NextDouble();
        }

        using(BinaryWriter binWriter = 
            new BinaryWriter(new MemoryStream()))
        {
            // Write the data to the stream.
            Console.WriteLine("Writing data to the stream.");
            for(i = 0; i < arrayLength; i++)
            {
                binWriter.Write(dataArray[i]);
            }

            // Create a reader using the stream from the writer.
            using(BinaryReader binReader = 
                new BinaryReader(binWriter.BaseStream))
            {
                try
                {
                    // Return to the beginning of the stream.
                    binReader.BaseStream.Position = 0;

                    // Read and verify the data.
                    Console.WriteLine("Verifying the written data.");
                    for(i = 0; i < arrayLength; i++)
                    {
                        if(binReader.ReadDouble() != dataArray[i])
                        {
                            Console.WriteLine("Error writing data.");
                            break;
                        }
                    }
                    Console.WriteLine("The data was written " +
                        "and verified.");
                }
                catch(EndOfStreamException e)
                {
                    Console.WriteLine("Error writing data: {0}.",
                        e.GetType().Name);
                }
            }
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

void main()
{
    int i;
    const int arrayLength = 1000;

    // Create random data to write to the stream.
    double dataArray __gc[] = new double __gc[arrayLength];
    Random* randomGenerator = new Random();
    for(i = 0; i < arrayLength; i++)
    {
        dataArray[i] = 100.1 * randomGenerator->NextDouble();
    }

    BinaryWriter* binWriter  = 
        new BinaryWriter(new MemoryStream());
    try
    {
        // Write data to the stream.
        Console::WriteLine(S"Writing data to the stream.");
        i = 0;
        for(i = 0; i < arrayLength; i++)
        {
                binWriter->Write(dataArray[i]);
        }

        // Create a reader using the stream from the writer.
        BinaryReader* binReader  = 
            new BinaryReader(binWriter->BaseStream);

        // Return to the beginning of the stream.
        binReader->BaseStream->Position = 0;

        try
        {
            // Read and verify the data.
            i = 0;
            Console::WriteLine(S"Verifying the written data.");
            for(i = 0; i < arrayLength; i++)
            {
                if(binReader->ReadDouble() != dataArray[i])
                {
                    Console::WriteLine(S"Error writing data.");
                    break;
                }
            }
            Console::WriteLine(S"The data was written and verified.");
        }
        catch(EndOfStreamException* e)
        {
            Console::WriteLine(S"Error writing data: {0}.",
                e->GetType()->Name);
        }
    }
    __finally
    {
        binWriter->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

Show:
© 2014 Microsoft