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:
sourceFileName: The path for the existing file.destFileName: The path to the new file that is being created.overwrite:trueto allow an existing file to be overwritten; otherwise,false.
Delete
Deletes a file.
public static void Delete(string path)
Parameters:
path: The path of the file to be deleted.
Exists
Determines whether a file exists.
public static bool Exists(string path)
Parameters:
path: The file to check.
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:
sourceFileName: The path for the existing file.destFileName: The path to the new file that is being created.
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:
path: The file to open for reading.
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:
path: The file to read.
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:
path: The file to read.
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:
path: The file to write to.bytes: The byte array to write to the file.
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:
path: The file to write to.contents: The collection of strings to write to the file.
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:
path: The file to write to.contents: The string to write to the file.