English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C# provides the following classes for the file system. They can be used to access directories, access files, open files for reading or writing, create new files, or move existing files from one location to another, etc.
Class Name | Usage |
---|---|
File | The File is a static class that provides different functions, such as copying, creating, moving, deleting, opening for reading or writing, encrypting or decrypting, checking if the file exists, adding lines or text to the file content, getting the last access time, etc. |
FileInfo | The FileInfo class provides the same functionality as the static File class. By manually writing code to read or write bytes from the file, you can better control the read/Write operations. |
Directory | The Directory is a static class that provides functions for creating, moving, deleting, and accessing subdirectories. |
DirectoryInfo | The DirectoryInfo provides instance methods for creating, moving, deleting, and accessing subdirectories. |
Path | The Path is a static class that provides some functions, such as retrieving the file extension, changing the file extension, retrieving the absolute physical path, and other related path functions. |
C# includes the static File class, used to perform i/Operations. The static File class includes various utility methods that can interact with any type of physical file (such as binary files, text files, etc.).
Use this static File class to perform some quick operations on physical files. Due to performance reasons, it is not recommended to use the File class for multiple operations on multiple files at the same time. In this case, use the FileInfo class.
Method | Usage |
---|---|
AppendAllLines | Append a line to the file and then close the file. If the specified file does not exist, this method will create a file, write the specified line to the file, and then close the file. |
AppendAllText | Open a file, append the specified string to the file, and then close the file. If the file does not exist, this method will create a file, write the specified string to the file, and then close the file. |
AppendText | Create a StreamWriter that will write UTF-8Attach the encoded text to an existing file, or if the specified file does not exist, attach to a new file. |
Copy | Copy the existing file to a new file. Overwriting files with the same name is not allowed. |
Create | Create or overwrite a file at the specified path. |
CreateText | Create or open a file for writing UTF-8The file encoding the text. |
Decrypt | Decrypt the file encrypted by the current account using the Encrypt method. |
Delete | Delete the specified file. |
Encrypt | Encrypt the file so that only the account used to encrypt the file can decrypt the file. |
Exists | Determine if the specified file exists. |
GetAccessControl | Get a FileSecurity object that encapsulates the access control list (ACL) entries of the specified file. |
Move | Move the specified file to a new location and provide the option to specify a new file name. |
Open | In the presence of read/Open FileStream on the specified path with write access. |
ReadAllBytes | Open a binary file, read the content of the file into a byte array, and then close the file. |
ReadAllLines | Open a text file, read all the lines of the file, and then close the file. |
ReadAllText | Open a text file, read all the lines of the file, and then close the file. |
Replace | Replace the content of the specified file with the content of another file, delete the original file, and create a backup of the replacement file. |
WriteAllBytes | Create a new file, write the specified byte array to the file, and then close the file. If the target file already exists, it will be overwritten. |
WriteAllLines | Create a new file, write the collection of strings to the file, and then close the file. |
WriteAllText | Create a new file, write the specified string to the file, and then close the file. If the target file already exists, it will be overwritten. |
Use the AppendAllLines() method to add multi-line text to a specified file, as shown below.
string dummyLines = "This is first line."; + Environment.NewLine + "This is second line." + Environment.NewLine + "This is third line."; //Open DummyFile.txt and append lines. If the file does not exist, create and open it. File.AppendAllLines(@"C:\DummyFile.txt", dummyLines.Split(Environment.NewLine.ToCharArray()).ToList<string>());
Use the File.AppendAllText() method to append a string to a file on a single line of code, as shown below.
//Open DummyFile.txt and append text. If the file does not exist, create and open it. File.AppendAllText(@"C:\ DummyFile.txt", "This is File testing");
Overwrite textUseThe method writes text to the file. Note that it does not append text, but overwrites the existing text.
Example: Overwrite existing text
//Open DummyFile.txt and write text. If the file does not exist, it will be created and opened. File.WriteAllText(@"C:\DummyFile.txt", "This is dummy text");
The following example demonstrates how to perform different operations using the static File class.
//Check if the file exists at a specific location bool isFileExists = File.Exists(@"C:\ DummyFile.txt"); // Return false //Copy DummyFile.txt to a new file DummyFileNew.txt File.Copy(@"C:\DummyFile.txt", @"D:\NewDummyFile.txt"); //Get the time of the last access to the file DateTime lastAccessTime = File.GetLastAccessTime(@"C:\DummyFile.txt"); //Get the time of the last write to the file DateTime lastWriteTime = File.GetLastWriteTime(@"C:\DummyFile.txt"); // Move the file to a new location File.Move(@"C:\DummyFile.txt", @"D:\DummyFile.txt"); //Open a file and return FileStream to read bytes from the file FileStream fs = File.Open(@"D:\DummyFile.txt", FileMode.OpenOrCreate); //Open a file and return StreamReader to read strings from the file StreamReader sr = File.OpenText(@"D:\DummyFile.txt"); //Delete file File.Delete(@"C:\DummyFile.txt");
It is easy to handle physical files using the static File class. However, if you want more flexibility, you can use the FileInfo class. Similarly, use the static Directory class to handle physical directories.
File is a static class that can read\write from physical files with less coding.
The static File class provides functionalities such as creation, reading\writing, copying, moving, and deleting for physical files.
The static Directory class provides functionalities such as creation, copying, moving, and deleting for physical directories with less coding.
The FileInfo and DirectoryInfo classes provide the same functionality as the static File and Directory classes.