Redigera

Dela via


File.ReadAllBytes(String) Method

Definition

Opens a binary file, reads the contents of the file into a byte array, and then closes the file.

public:
 static cli::array <System::Byte> ^ ReadAllBytes(System::String ^ path);
public static byte[] ReadAllBytes (string path);
static member ReadAllBytes : string -> byte[]
Public Shared Function ReadAllBytes (path As String) As Byte()

Parameters

path
String

The file to open for reading.

Returns

Byte[]

A byte array containing the contents of the file.

Exceptions

.NET Framework and .NET Core versions older than 2.1: path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.

path is null.

The specified path, file name, or both exceed the system-defined maximum length.

The specified path is invalid (for example, it is on an unmapped drive).

An I/O error occurred while opening the file.

This operation is not supported on the current platform.

-or-

path specified a directory.

-or-

The caller does not have the required permission.

The file specified in path was not found.

path is in an invalid format.

The caller does not have the required permission.

Remarks

Given a file path, this method opens the file, reads the contents of the file into a byte array, and then closes the file.

Applies to

See also