.NET API Documentation

Microsoft Developer Network

File Class - System.IO

Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.

Namespace: System.IO
Assembly: System.Runtime.Extensions (.NET Core, .NET 5+), mscorlib.dll (.NET Framework)

Syntax

public static class File

The File class is a static class, meaning you do not instantiate it. You call its methods directly using the class name, like File.Exists(...).

Methods

Name Description
📄 Copy Copies an existing file to a new location.
🗑 Delete Deletes a file.
🔍 Exists Determines whether a file exists.
📝 Move Moves an existing file to a new location.
📁 ReadAllBytes Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
📁 ReadAllLines Opens a text file, reads all lines of the file into a string array, and then closes the file.
📁 ReadAllText Opens a text file, reads all lines of the file, and then closes the file.
📂 WriteAllBytes 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.
📂 WriteAllLines Creates a new file, writes a collection of strings to the file, and then closes the file. Each string is written to the file as a separate line.
📂 WriteAllText Opens a file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

Details

Copy

Copies an existing file to a new location.

public static void Copy(string sourceFileName, string destFileName)
public static void Copy(string sourceFileName, string destFileName, bool overwrite)

Parameters:

Delete

Deletes a file.

public static void Delete(string path)

Parameters:

Exists

Determines whether a file exists.

public static bool Exists(string path)

Parameters:

Returns: true if the caller has the required permissions and path contains a file; otherwise, false. This method also returns false if any error occurs while accessing the file.

Move

Moves an existing file to a new location.

public static void Move(string sourceFileName, string destFileName)

Parameters:

Note: If destFileName already exists, an exception will be thrown.

ReadAllBytes

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

public static byte[] ReadAllBytes(string path)

Parameters:

Returns: A byte array containing the contents of the file.

ReadAllLines

Opens a text file, reads all lines of the file into a string array, and then closes the file.

public static string[] ReadAllLines(string path)

Parameters:

Returns: A string array containing all lines of the file.

ReadAllText

Opens a text file, reads all lines of the file, and then closes the file.

public static string ReadAllText(string path)

Parameters:

Returns: A string containing all the content of the file.

WriteAllBytes

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(string path, byte[] bytes)

Parameters:

WriteAllLines

Creates a new file, writes a collection of strings to the file, and then closes the file. Each string is written to the file as a separate line.

public static void WriteAllLines(string path, IEnumerable contents)

Parameters:

WriteAllText

Opens a file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

public static void WriteAllText(string path, string contents)

Parameters: