Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ReadAllBytes Method (My.Computer.FileSystem Object)
Collapse the table of content
Expand the table of content

My.Computer.FileSystem.ReadAllBytes Method 

Returns the contents of a file as a byte array.

' Usage
Dim value As Byte() = My.Computer.FileSystem.ReadAllBytes(file)
' Declaration
Public Function ReadAllBytes( _
   ByVal file As String _
) As Byte()



String. File to be read. Required.

Byte array containing the contents of the file.

The following conditions may cause an exception to be thrown:

  • The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\) (ArgumentException).

  • The file name ends with a trailing slash (ArgumentException).

  • The path is not valid because it is Nothing (ArgumentNullException).

  • The file does not exist (FileNotFoundException).

  • The file is in use by another process or an I/O error occurs (IOException).

  • The path exceeds the system-defined maximum length (PathTooLongException).

  • A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).

  • There is not enough memory to write the string to buffer (OutOfMemoryException).

  • The user lacks necessary permissions to view the path (SecurityException).

The ReadAllBytes method of the My.Computer.FileSystem object allows you to read from a binary file. The contents of the file are returned as a byte array.

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 following table lists examples of tasks involving the My.Computer.FileSystem.ReadAllBytes method.

To See

Read from a binary file

How to: Read From Binary Files in Visual Basic

This example reads from the file C:/Documents and Settings/selfportrait.jpg.

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

Namespace: Microsoft.VisualBasic.MyServices

Class: FileSystemProxy (provides access to FileSystem)

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type Available

Windows Application


Class Library


Console Application


Windows Control Library


Web Control Library


Windows Service


Web Site


The following permission may be necessary:

Permission Description


Controls the ability to access files and folders. Associated enumeration: Unrestricted.

For more information, see Code Access Security and Requesting Permissions.

Community Additions

© 2015 Microsoft