Reading – This operation is the basic read operation wherein data is read from a file. Writing – This operation is the basic write operation wherein data is written to a file. By default, all existing contents are removed from the file, and new content is written. Appending – This operation also involves writing information to a file. The only difference is that the existing data in a file is not overwritten. The new data to be written is added at the end of the file.
In this tutorial, you will learn-
File.Exists File.ReadAlllines File.ReadAllText File.Copy File.Delete
Basics I/O Commands
C# and .Net can work with files with the help of several File I/O commands. Let’s have a look at some of these commands. For our example, we will assume that we have a file in the D drive called Example.txt. The file will be a simple text file and have 2 lines as shown below
Guru99 – .Net Guru99 -C#
For our example, we will create a simple Console application and work with our File I/O commands. The console application is the basic one which was created in the earlier tutorial. In the console application, all code is written to the program.cs file.
File.Exists
The File exists method is used to check if a particular file exists. So now let’s see the code which can be used to check if our Example.txt file exists or not. Enter the below code in the program.cs file.
Code Explanation:-
First, we are setting a string variable with the path to our Example.txt file. Next, we use the File.Exists method to check if the file exists or not. If the File exists, a true value will be returned. If we get a true value and the file does exist, then we write the message “File Exists” to the console.
When the above code is set, and the project is executed using Visual Studio, you will get the below output.
Output:-
From the above output, you can see that the File.Exists command was executed successfully, and the correct message was displayed in the console window.
File.ReadAlllines
The method is used to read all the lines one by one in a file. The lines are then stored in a string array variable. Let’s look at an example. Enter the below code in the program.cs file.
Code Explanation:-
First, we are declaring a string array variable. This will be used to store the result which will be returned by the File.ReadAllLines method. Next, we use the File.ReadAllLines method to read all the lines from our text file. The result is then passed to the lines variable. Since we know that our file contains only 2 lines, we can access the value of the array variables via the lines[0] and lines[1] command.
When the above code is set, and the project is run using Visual Studio, you will get the below output.
Output:-
From the output, you can see that the File.ReadAllLines command returned both the lines from our file Example.txt
File.ReadAllText
This method is used to read all the lines in a file at once. The lines are then stored in a string variable. Let’s look at an example. Enter the below code in the program.cs file.
Code Explanation:-
First, we are declaring a string variable called Lines. This will be used to store the result which will be returned by the File.ReadAllText method. Next, we use the File.ReadAllText method to read all the lines from our text file. The result is then passed to the lines variable. We can directly use the Console.Writeline method to display the value of the Lines variable.
When the above code is set, and the project is run using Visual Studio, you will get the below output.
Output:-
From the output, you can see that the File.ReadAlltext command returned both the lines from our file Example.txt
File.Copy
The method is used to make a copy of an existing file. Let’s look at an example. Enter the below code in the program.cs file.
Code Explanation:-
First, we are declaring a string variable called path. This will be the location of our Example.txt file. This file will be the source file used for the copy operation. Next, we are declaring a string variable called copypath. This will be the location of a new file called ExampleNew.txt file. This will be the destination file in which the contents will be written from the source file Example.txt. We then call the File.Copy method to copy the file Example.txt file to the file ExampleNew.txt.
When the above code is set, and the project is run using Visual Studio, the file Example.txt will be copied to ExampleNew.txt.
File.Delete
The method is used to delete an existing file. Let’s look at an example. Enter the below code in the program.cs file.
Code Explanation:-
First, we are declaring a string variable called path. This will be the location of our Example.txt file. This is the file which will be deleted. Next, we are calling the File.Delete method to delete the file.
When the above code is set, and the project is run using Visual Studio, the file Example.txt will be deleted from the D drive.
Summary
C# has a number of File operations which can be performed on files. Most of these operations are part of the class File. If you want to read data from a file, you can use the File.ReadAlltext or File.ReadAllLines methods.