File.WriteAllBytes(String, Byte[]) Method

Definition

Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.

public:
 static void WriteAllBytes(System::String ^ path, cli::array <System::Byte> ^ bytes);
public static void WriteAllBytes (string path, byte[] bytes);
static member WriteAllBytes : string * byte[] -> unit
Public Shared Sub WriteAllBytes (path As String, bytes As Byte())

Parameters

path
String

The file to write to.

bytes
Byte[]

The bytes to write to the file.

Exceptions

path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

path is null or the byte array is empty.

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.

path specified a file that is read-only.

-or-

path specified a file that is hidden.

-or-

This operation is not supported on the current platform.

-or-

path specified a directory.

-or-

The caller does not have the required permission.

path is in an invalid format.

The caller does not have the required permission.

Remarks

Given a byte array and a file path, this method opens the specified file, writes the contents of the byte array to the file, and then closes the file.

Security

FileIOPermission
for access to write to a file or directory. Associated enumeration: Write

Applies to