BaseStream Property
Collapse the table of content
Expand the table of content

BinaryWriter.BaseStream Property


Gets the underlying stream of the BinaryWriter.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

public virtual Stream BaseStream { get; }

Property Value

Type: System.IO.Stream

The underlying stream associated with the BinaryWriter.

For a list of common I/O tasks, see Common I/O Tasks.

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.

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

            // Create a reader using the stream from the writer.
            using(BinaryReader binReader = 
                new BinaryReader(binWriter.BaseStream))
                    // 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.");
                    Console.WriteLine("The data was written " +
                        "and verified.");
                catch(EndOfStreamException e)
                    Console.WriteLine("Error writing data: {0}.",

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft