• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • AI
  • Blog

C# has a wide array of file operations. These operations include opening a file, reading or writing to a file. There can be instances wherein you want to work with files directly, in which case you would use the file operations available in C#. Some of the basic file operations are mentioned below.

  1. Reading – This operation is the basic read operation wherein data is read from a file.
  2. 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.
  3. 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-

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.

C# File Operations

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
 class Tutorial
 {
  static void Main(string[] args)
  {
   String path = @"D:\Example.txt";
   
   if (File.Exists(path))
   {
    Console.WriteLine("File Exists");
   }
   Console.ReadKey();
  }
 }
}

Code Explanation:-

  1. First, we are setting a string variable with the path to our Example.txt file.
  2. 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.
  3. 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:-

C# File Operations

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.

C# File Operations

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
 class Tutorial
 {
  static void Main(string[] args)
  {
   String path = @"D:\Example.txt";

   String[] lines;
   lines = File.ReadAllLines(path);

   Console.WriteLine(lines[0]);
   Console.WriteLine(lines[1]);

   Console.ReadKey();
  }
 }
}

Code Explanation:-

  1. 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.
  2. 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.
  3. 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:-

C# File Operations

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.

C# File Operations

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
  class Tutorial
  {
   static void Main(string[] args)
   {
    String path = @"D:\Example.txt";

    String lines;
    lines = File.ReadAllText(path);
    Console.WriteLine(lines);
 
    Console.ReadKey();
   }
  }
}

Code Explanation:-

  1. 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.
  2. 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.
  3. 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:-

C# File Operations

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.

C# File Operations

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
  class Tutorial
  {
   static void Main(string[] args)
   {
   String path = @"D:\Example.txt";

   String copypath = @"D:\ExampleNew.txt";

   File.Copy(path,copypath);

   Console.ReadKey();
   }
  }
}

Code Explanation:-

  1. 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.
  2. 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.
  3. 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.

C# File Operations

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
  class Tutorial
  {
   static void Main(string[] args)
   {
   String path = @"D:\Example.txt";

   File.Delete(path);

   Console.ReadKey();
   }
  }
}

Code Explanation:-

  1. 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.
  2. 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.
File Method Description
File.Exists File exists method is used to check if a particular file exists.
File.ReadAlllines The method is used to read all the lines one by one in a file.
File.ReadAllText This method is used to read all the lines in a file at once.
File.Copy The method is used to make a copy of an existing file.
File.Delete The method is used to delete an existing file.

 

YOU MIGHT LIKE: