Windows apps
Collapse the table of content
Expand the table of content
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.

BinaryWriter.Write Method (Byte[], Int32, Int32)

.NET Framework 1.1

Writes a region of a byte array to the current stream.

[Visual Basic]
Overloads Public Overridable Sub Write( _
   ByVal buffer() As Byte, _
   ByVal index As Integer, _
   ByVal count As Integer _
)
[C#]
public virtual void Write(
 byte[] buffer,
 int index,
 int count
);
[C++]
public: virtual void Write(
 unsigned char buffer __gc[],
 int index,
 int count
);
[JScript]
public function Write(
   buffer : Byte[],
 index : int,
 count : int
);

Parameters

buffer
A byte array containing the data to write.
index
The starting point in buffer at which to begin writing.
count
The number of bytes to write.

Exceptions

Exception Type Condition
ArgumentException The buffer length minus index is less than count.
ArgumentNullException buffer is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException index or count is negative.
IOException An I/O error occurs.
ObjectDisposedException The stream is closed.

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, 0, dataArray.Length)

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

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

        ' Read and verify the data.
        Dim verifyArray(upperBound) As Byte
        If binReader.Read(verifyArray, 0, dataArray.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, 0, arrayLength);

        // 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 = new byte[arrayLength];
        if(binReader.Read(verifyArray, 0, arrayLength) != 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(S"Writing the data.");
    binWriter->Write(dataArray, 0, arrayLength);

    // 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 __gc[] = new Byte __gc[arrayLength];
    if(binReader->Read(
        verifyArray, 0, arrayLength) != 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

BinaryWriter Class | BinaryWriter Members | System.IO Namespace | BinaryWriter.Write Overload List | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2017 Microsoft