How To Use File Class In C#

In this blog, I am going to share with you how to use a File Class. File class helps us to perform various operations over a file, like create, delete, read and write etc. Operation on file is the most important activity while writing a program. We use a file to save some data or information. Here, I am going to explain how we can use File class in our application to perform various operations. In the below figure 1, I have used four buttons and one textbox to perform write, read, and delete operations.

Write Operation Using file Class

Step 1

File class is available inside System.IO Namespace. In order to use the File class, first, you need to use the namespace.

Step 2

Write code on the write button click as shown in the below code. WriteAllText is a static method which is available in the File class. This method takes two parameters - location where you want to write, and content that you want to write. The method creates a file if the filename does not exist in the given location.

  1. //Write contents on notepad inside the debug folder of application  
  2.    private void button1_Click(object sender, EventArgs e)  
  3.    {  
  4.        File.WriteAllText(Application.StartupPath + @"\demo.txt", textBox1.Text);  
  5.    }  

Read Operation Using file Class

Step 1

File class is available inside System.IO Namespace. In order to use the File class, first, you need to use the namespace.

Step 2

Write code for the Read button click event as shown in the below code. ReadAllText is a static method which is available in the File class.It takes one parameter, i.e., location from where you want to read the data. ReadAllText returns string value.

  1. //Read contents from demo.txt file   
  2.         private void button2_Click(object sender, EventArgs e)  
  3.         {  
  4.            string allText= File.ReadAllText(Application.StartupPath + @"\demo.txt");  
  5.         }  

Check File exists  or not Using file Class

Step 1

File class is available inside System.IO Namespace. In order to use the File class, first, you need to use the namespace.

Step 2

Write code on Check button click event as shown in the below code. Exists  is a static method which is available in the File class. It takes one parameter - Location. The file Exists method returns bool value. It returns true if the file will available at the target location otherwise false.

  1. //Delete the existing File   
  2.         private void button3_Click(object sender, EventArgs e)  
  3.         {  
  4.            if( File.Exists(Application.StartupPath + @"\demo.txt"))  
  5.            {  
  6.                File.Delete(Application.StartupPath + @"\demo.txt");  
  7.   
  8.            }  
  9.             else  
  10.            {  
  11.   
  12.                MessageBox.Show("File Not Found..");  
  13.            }  
  14.         }  

Delete Operation Using file Class

Step 1

File class is available inside System.IO Namespace. In order to use the File class, first, you need to use the namespace.

Step 2

Write code on Delete button click event as shown in the below code. Delete is a static method which is available inside the File class. It method takes one parameter, the parameter is location from where you want to delete the file. Delete method returns void value. I used Exists method to check whether file exist or not at the target location.

  1. //Delete the existing File   
  2.         private void button3_Click(object sender, EventArgs e)  
  3.         {  
  4.            if( File.Exists(Application.StartupPath + @"\demo.txt"))  
  5.            {  
  6.                File.Delete(Application.StartupPath + @"\demo.txt");  
  7.   
  8.            }  
  9.             else  
  10.            {  
  11.   
  12.                MessageBox.Show("File Not Found..");  
  13.            }  
  14.         }