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

BinaryReader.ReadBytes Method

Reads count bytes from the current stream into a byte array and advances the current position by count bytes.

[Visual Basic]
Public Overridable Function ReadBytes( _
   ByVal count As Integer _
) As Byte()
[C#]
public virtual byte[] ReadBytes(
 int count
);
[C++]
public: virtual unsigned char ReadBytes(
 int count
)  __gc[];
[JScript]
public function ReadBytes(
   count : int
) : Byte[];

Parameters

count
The number of bytes to read.

Return Value

A byte array containing data read from the underlying stream. This might be less than the number of bytes requested if the end of the stream is reached.

Exceptions

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

Remarks

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
Append text to a file. Opening and Appending to a Log File

File.AppendText

FileInfo.AppendText

Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
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 write binary data using memory as a backing store, and then verify that the data was written correctly.

[Visual Basic] 
Imports System
Imports System.IO

Public Class BinaryRW

    Shared Sub Main()
    
        Const upperBound As Integer = 1000

        ' Create random data to write to the stream.
        Dim dataArray(upperBound) As Byte
        Dim randomGenerator As New Random
        randomGenerator.NextBytes(dataArray)

        Dim binWriter As New BinaryWriter(New MemoryStream())

        ' Write the data to the stream.
        Console.WriteLine("Writing the data.")
        binWriter.Write(dataArray)

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

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

        ' Read and verify the data.
        Dim verifyArray() As Byte = _
            binReader.ReadBytes(dataArray.Length)
        If verifyArray.Length <> dataArray.Length Then
            Console.WriteLine("Error writing the data.")
            Return
        End If
        For i As Integer = 0 To upperBound
            If verifyArray(i) <> dataArray(i) Then
                Console.WriteLine("Error writing the data.")
                Return
            End If
        Next i
        Console.WriteLine("The data was written and verified.")
    
    End Sub
End Class

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

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

        // Create random data to write to the stream.
        byte[] dataArray = new byte[arrayLength];
        new Random().NextBytes(dataArray);

        BinaryWriter binWriter = new BinaryWriter(new MemoryStream());

        // Write the data to the stream.
        Console.WriteLine("Writing the data.");
        binWriter.Write(dataArray);

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

        // Set Position to the beginning of the stream.
        binReader.BaseStream.Position = 0;

        // Read and verify the data.
        byte[] verifyArray = binReader.ReadBytes(arrayLength);
        if(verifyArray.Length != arrayLength)
        {
            Console.WriteLine("Error writing the data.");
            return;
        }
        for(int i = 0; i < arrayLength; i++)
        {
            if(verifyArray[i] != dataArray[i])
            {
                Console.WriteLine("Error writing the data.");
                return;
            }
        }
        Console.WriteLine("The data was written and verified.");
    }
}

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

void main()
{
    const int arrayLength = 1000;

    // Create random data to write to the stream.
    Byte dataArray __gc[] = new Byte __gc[arrayLength];
    (new Random())->NextBytes(dataArray);

    BinaryWriter* binWriter = new BinaryWriter(new MemoryStream());

    // Write the data to the stream.
    Console::WriteLine("Writing the data.");
    binWriter->Write(dataArray);

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

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

    // Read and verify the data.
    Byte verifyArray __gc[] = binReader->ReadBytes(arrayLength);
    if(verifyArray->Length != arrayLength)
    {
        Console::WriteLine(S"Error writing the data.");
        return;
    }
    for(int i = 0; i < arrayLength; i++)
    {
        if(verifyArray[i] != dataArray[i])
        {
            Console::WriteLine(S"Error writing the data.");
            return;
        }
    }
    Console::WriteLine(S"The data was written and verified.");
}

[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.