C # contains the static file class for performing I/O operations on the physical file system. The static file class contains several functions for interacting with, for example, a physical file of any type. binary, text, etc.
The File
class comes under the System.IO
namespace. Let's discuss some examples of File Class.
In C# for append multiple text lines in a file, there's a method File.AppendLine()
.
string sampleText = "This is first line. \n This is second line. \nThis is third line.";
// If the file does not exist then create and open the file and append lines.
// If the file exists then Open the file and append lines.
File.AppendAllLines(@"E:\Example.txt", sampleText.Split(Environment.NewLine.ToCharArray()).ToList<string>());
In C# for append a line of string in the file there's a method File.AppendAllText();
// If the file does not exist then create and open the file and append text.
// If the file exists then Open the file and append text.
File.AppendAllText(@"E:\Example.txt", "New Text");
To remove the existing text and overwrite new text in the file there's a method File.WriteAllText();
// If the file does not exist then create and open the file and write text.
// If the file exists then Open the file and write text.
File.WriteAllText(@"E:\Example.txt", "Remove All the previous Text");
//Copy an existing file to a new file.
File.Copy(@"C:\Example.txt", @"D:\Newfile.txt");
//Get the file last accessed last time
DateTime lastAccessTime = File.GetLastAccessTime(@"C:\Example.txt");
//Get when the file was written last time
DateTime lastWriteTime = File.GetLastWriteTime(@"C:\Example.txt");
// Move a specified file to new location
File.Move(@"C:\Example.txt", @"D:\Example.txt");
//Opens a File and returns FileStream
FileStream fs = File.Open(@"D:\Example.txt", FileMode.OpenOrCreate);
//Opens a file and return StreamReader
StreamReader sr = File.OpenText(@"D:\Example.txt");
//To check whether file is exists or not
bool isFileExists = File.Exists(@"C:\ Example.txt"); // returns false
//Delete a file
File.Delete(@"C:\Example.txt");
Method | Usage |
---|---|
AppendLine |
Appends lines to a file, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file. |
AppendAllLines |
Appends lines to a file by using a specified encoding, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file. |
AppendAllLinesAsync |
Asynchronously appends lines to a file, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file. |
AppendAllText |
Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file. |
AppendAllTextAsync |
Asynchronously opens a file or creates a file if it does not already exist, appends the specified string to the file, and then closes the file. |
AppendText |
Creates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist. |
Copy |
Copies an existing file to a new file. Overwriting a file of the same name is not allowed. |
Create |
Creates or overwrites a file in the specified path. |
Move |
Moves a specified file to a new location, providing the option to specify a new file name. |
Open |
Opens a FileStream on the specified path with read/write access with no sharing. |
OpenText |
Opens an existing UTF-8 encoded text file for reading. |
OpenWrite |
Opens an existing file or creates a new file for writing. |
ReadAllLines |
Opens a text file, reads all lines of the file, and then closes the file. |
Replace |
Replaces the contents of a specified file with the contents of another file, deleting the original file, and creating a backup of the replaced file. |
WriteAllText |
Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten. |