How to: Write to Binary Files in Visual Basic

The WriteAllBytes method writes data to a binary file. If the append parameter is True, it will append the data to the file; otherwise data in the file is overwritten.

If the specified path excluding the file name is not valid, a DirectoryNotFoundException exception will be thrown. If the path is valid but the file does not exist, the file will be created.

To write to a binary file

Use the WriteAllBytes method, supplying the file path and name and the bytes to be written. This example appends the data array CustomerData to the file named CollectedData.dat.

Dim CustomerData As Byte() = (From c In customerQuery).ToArray()

My.Computer.FileSystem.WriteAllBytes(
  "C:\MyDocuments\CustomerData", CustomerData, True)

Robust Programming

The following conditions may create an exception:

See also