This documentation is archived and is not being maintained.

How to: Read From Binary Files in Visual Basic

The My.Computer.FileSystem object provides the ReadAllBytes method for reading from binary files.

To read from a binary file

  • Use the ReadAllBytes method, which returns the contents of a file as a byte array. This example reads from the file C:/Documents and Settings/selfportrait.jpg.

    My.Computer.FileSystem.ReadAllBytes _
    ("C:/Documents and Settings/selfportrait.jpg")

The following conditions may cause an exception to be thrown:

Do not make decisions about the contents of the file based on the name of the file. For example, the file Form1.vb may not be a Visual Basic source file.

Verify all inputs before using the data in your application. The contents of the file may not be what is expected, and methods to read from the file may fail.