Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Writing Binary Data

You use the BinaryWriter class to write binary data to a file. Like BinaryReader, BinaryWriter must be associated with a FileStream object that's connected to the file. Here's how (assuming that fs refers to the FileStream ):

Dim bw As BinaryWriter
bw = New BinaryWriter(fs)

To write a single item of data to the file, use the Write() method. You can write any of .NET's simple datatypes, such as Char, Integer, or Double, with this method:

bw.Write( variable )

You can also write part or all of a byte or character array to the file. To write the entire array, pass the array name as the argument to Write(). To write part of the array, use this syntax (see Table 6 for the arguments):

bw.Write(array , index , coun)

Table 6 Write() Arguments.

Argument

Description

array

The name of the array.

index

The array position at which to start.

count

The number of array elements to write to the file.

If count + index is greater than the length of the array, an exception is thrown.

When finished writing data, be sure to call the BinaryWriter.Close() method to close the writer and the underlying FileStream.

  • + Share This
  • 🔖 Save To Your Account