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)

.NET Framework 1.1

Writes an unsigned byte to the current stream and advances the stream position by one byte.

[Visual Basic]
Overloads Public Overridable Sub Write( _
   ByVal value As Byte _
)
[C#]
public virtual void Write(
 byte value
);
[C++]
public: virtual void Write(
 unsigned char value
);
[JScript]
public function Write(
   value : Byte
);

Parameters

value
The unsigned byte to write.

Exceptions

Exception Type Condition
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()
    
        Dim i As Integer = 0

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

        Dim binWriter As New BinaryWriter(New MemoryStream())
        Dim binReader As New BinaryReader(binWriter.BaseStream)

        Try
        
            ' Write the data to the stream.
            Console.WriteLine("Writing the data.")
            For i = 0 To writeArray.Length - 1
                binWriter.Write(writeArray(i))
            Next i

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

            ' Read and verify the data from the stream.
            For i = 0 To writeArray.Length - 1
                If binReader.ReadByte() <> writeArray(i) Then
                    Console.WriteLine("Error writing the data.")
                    Return
                End If
            Next i
            Console.WriteLine("The data was written and verified.")

        ' Catch the EndOfStreamException and write an error message.
        Catch ex As EndOfStreamException
            Console.WriteLine("Error writing the data: {0}", _
                ex.GetType().Name)
        End Try
    
    End Sub
End Class

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

class BinaryRW
{
    static void Main()
    {
        int i = 0;

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

        BinaryWriter binWriter = new BinaryWriter(new MemoryStream());
        BinaryReader binReader = 
            new BinaryReader(binWriter.BaseStream);

        try
        {
            // Write the data to the stream.
            Console.WriteLine("Writing the data.");
            for(i = 0; i < writeArray.Length; i++)
            {
                binWriter.Write(writeArray[i]);
            }

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

            // Read and verify the data from the stream.
            for(i = 0; i < writeArray.Length; i++)
            {
                if(binReader.ReadByte() != writeArray[i])
                {
                    Console.WriteLine("Error writing the data.");
                    return;
                }
            }
            Console.WriteLine("The data was written and verified.");
        }

        // Catch the EndOfStreamException and write an error message.
        catch(EndOfStreamException e)
        {
            Console.WriteLine("Error writing the data.\n{0}",
                e.GetType().Name);
        }
    }
}

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

void main()
{
    int i = 0;

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

    BinaryWriter* binWriter = new BinaryWriter(new MemoryStream());
    BinaryReader* binReader = 
        new BinaryReader(binWriter->BaseStream);

    try
    {
        // Write the data to the stream.
        Console::WriteLine(S"Writing the data.");
        for(i = 0; i < writeArray->Length; i++)
        {
            binWriter->Write(writeArray[i]);
        }

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

        // Read and verify the data from the stream.
        for(i = 0; i < writeArray->Length; i++)
        {
            if(binReader->ReadByte() != writeArray[i])
            {
                Console::WriteLine(S"Error writing the data.");
                return;
            }
        }
        Console::WriteLine(S"The data was written and verified.");
    }

    // Catch the EndOfStreamException and write an error message.
    catch(EndOfStreamException* e)
    {
        Console::WriteLine(S"Error writing the data.\n{0}",
            e->GetType()->Name);
    }
}

[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:
© 2014 Microsoft