Share via


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")
    

Robust Programming

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.

See Also

Tasks

How to: Read From Text Files with Multiple Formats in Visual Basic

Reference

My.Computer.FileSystem.ReadAllBytes Method

My.Computer.FileSystem.WriteAllBytes Method

Other Resources

Reading from Files in Visual Basic

Storing Data to and Reading From the Clipboard