---
description: C# has a wide array of file operations. These operations include opening a file, reading or writing to a file. There can be an instances wherein you want to work with files directly, in which case you
title: File Handling in C#: I/O Operations [Examples]
image: https://www.guru99.com/images/c-sharp-file-handling-io.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

C# file operations let you read, write, copy, and delete files using the System.IO namespace. Here you will learn to use File.Exists, File.ReadAllLines, File.ReadAllText, File.Copy, and File.Delete with clear code examples.

* 📂 **What it is:** C# file operations read, write, copy, and delete files through the System.IO namespace.
* ✅ **File.Exists:** File.Exists(path) checks whether a file is present before you read or delete it.
* 📖 **Reading:** File.ReadAllLines returns a string array, while File.ReadAllText returns the whole file as one string.
* 📋 **Copy & delete:** File.Copy duplicates a file to a new path and File.Delete removes it.
* 🤖 **AI help:** AI tools like GitHub Copilot generate C# file-handling code instantly.

[ Read More ](javascript:void%280%29;) 

![C# File Operations](https://www.guru99.com/images/c-sharp-file-handling-io.png)

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.

## 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.

[](https://www.guru99.com/images/c-sharp-net/052716%5F0700%5FCFileOperat1.png)

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:-

[](https://www.guru99.com/images/c-sharp-net/052716%5F0700%5FCFileOperat2.png)

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.

### RELATED ARTICLES

* [C# Inheritance and Polymorphism with Program Examples ](https://www.guru99.com/c-sharp-inheritance-polymorphism.html "C# Inheritance and Polymorphism with Program Examples")
* [Abstract Class in C# with Example ](https://www.guru99.com/c-sharp-abstract-class.html "Abstract Class in C# with Example")
* [Top 50 C# Interview Questions and Answers (2026) ](https://www.guru99.com/c-sharp-interview-questions.html "Top 50 C# Interview Questions and Answers (2026)")
* [13 BEST C# Books (2026 Update) ](https://www.guru99.com/best-c-sharp-books.html "13 BEST C# Books (2026 Update)")

## 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.

[](https://www.guru99.com/images/c-sharp-net/052716%5F0700%5FCFileOperat3.png)

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](https://www.guru99.com/download-install-visual-studio.html), you will get the below output.

### Output:-

[](https://www.guru99.com/images/c-sharp-net/052716%5F0700%5FCFileOperat4.png)

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.

[](https://www.guru99.com/images/c-sharp-net/052716%5F0700%5FCFileOperat5.png)

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:-

[](https://www.guru99.com/images/c-sharp-net/052716%5F0700%5FCFileOperat6.png)

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.

[](https://www.guru99.com/images/c-sharp-net/052716%5F0700%5FCFileOperat7.png)

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.

[](https://www.guru99.com/images/c-sharp-net/052716%5F0700%5FCFileOperat8.png)

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.

## C# File Methods Reference

The table below summarizes the main C# file methods and what each one does.

| 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.                   |

## FAQs

📂 What are file operations in C#?

C# file operations let you read, write, copy, append, and delete files. Most methods belong to the System.IO.File class, such as File.Exists, File.ReadAllText, File.Copy, and File.Delete.

✅ How do you check if a file exists in C#?

Use File.Exists(path), which returns true when the file exists and false otherwise. Check it before reading or deleting a file to avoid runtime exceptions.

📖 What is the difference between File.ReadAllLines and File.ReadAllText?

File.ReadAllLines returns each line as an element of a string array, while File.ReadAllText returns the entire file content as a single string.

📋 How do you copy a file in C#?

Call File.Copy(sourcePath, destinationPath). It creates a new file at the destination containing the contents of the source file.

🗑️ How do you delete a file in C#?

Call File.Delete(path). It permanently removes the file, so confirm it exists with File.Exists first to avoid a FileNotFoundException.

🤖 How can AI tools like GitHub Copilot help with C# file operations?

AI assistants like GitHub Copilot autocomplete File.ReadAllText, File.Copy, and File.Delete calls and generate try-catch file-handling code from a short comment describing the task.

🧠 Can AI automate C# file handling code?

Yes. AI-powered tools generate reader and writer methods, suggest StreamReader or File methods, and add exception handling so your code is safer.

📁 Which namespace is needed for file operations in C#?

Add using System.IO; at the top of your program. The System.IO namespace contains the File, StreamReader, and StreamWriter classes used for file handling.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/c-sharp-file-handling-io.png","url":"https://www.guru99.com/images/c-sharp-file-handling-io.png","width":"700","height":"250","caption":"C# File Handling (I/O)","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/c-sharp-file-operations.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/c","name":"C#"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/c-sharp-file-operations.html","name":"File Handling in C#: I/O Operations [Examples]"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/c-sharp-file-operations.html#webpage","url":"https://www.guru99.com/c-sharp-file-operations.html","name":"File Handling in C#: I/O Operations [Examples]","dateModified":"2026-07-11T18:24:19+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/c-sharp-file-handling-io.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/c-sharp-file-operations.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/benjamin","name":"Benjamin Walker","description":"I'm Benjamin Walker, an expert in C, C++, and C# programming, providing resources to enhance your coding proficiency and project outcomes.","url":"https://www.guru99.com/author/benjamin","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/benjamin-walker-author.png","url":"https://www.guru99.com/images/benjamin-walker-author.png","caption":"Benjamin Walker","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"C#","headline":"File Handling in C#: I/O Operations [Examples]","description":"C# has a wide array of file operations. These operations include opening a file, reading or writing to a file. There can be an instances wherein you want to work with files directly, in which case you","keywords":"c#","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/benjamin","name":"Benjamin Walker"},"dateModified":"2026-07-11T18:24:19+05:30","image":{"@id":"https://www.guru99.com/images/c-sharp-file-handling-io.png"},"copyrightYear":"2026","name":"File Handling in C#: I/O Operations [Examples]","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What are file operations in C#?","acceptedAnswer":{"@type":"Answer","text":"C# file operations let you read, write, copy, append, and delete files. Most methods belong to the System.IO.File class, such as File.Exists, File.ReadAllText, File.Copy, and File.Delete."}},{"@type":"Question","name":"How do you check if a file exists in C#?","acceptedAnswer":{"@type":"Answer","text":"Use File.Exists(path), which returns true when the file exists and false otherwise. Check it before reading or deleting a file to avoid runtime exceptions."}},{"@type":"Question","name":"What is the difference between File.ReadAllLines and File.ReadAllText?","acceptedAnswer":{"@type":"Answer","text":"File.ReadAllLines returns each line as an element of a string array, while File.ReadAllText returns the entire file content as a single string."}},{"@type":"Question","name":"How do you copy a file in C#?","acceptedAnswer":{"@type":"Answer","text":"Call File.Copy(sourcePath, destinationPath). It creates a new file at the destination containing the contents of the source file."}},{"@type":"Question","name":"How do you delete a file in C#?","acceptedAnswer":{"@type":"Answer","text":"Call File.Delete(path). It permanently removes the file, so confirm it exists with File.Exists first to avoid a FileNotFoundException."}},{"@type":"Question","name":"How can AI tools like GitHub Copilot help with C# file operations?","acceptedAnswer":{"@type":"Answer","text":"AI assistants like GitHub Copilot autocomplete File.ReadAllText, File.Copy, and File.Delete calls and generate try-catch file-handling code from a short comment describing the task."}},{"@type":"Question","name":"Can AI automate C# file handling code?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI-powered tools generate reader and writer methods, suggest StreamReader or File methods, and add exception handling so your code is safer."}},{"@type":"Question","name":"Which namespace is needed for file operations in C#?","acceptedAnswer":{"@type":"Answer","text":"Add using System.IO; at the top of your program. The System.IO namespace contains the File, StreamReader, and StreamWriter classes used for file handling."}}]}],"@id":"https://www.guru99.com/c-sharp-file-operations.html#schema-1142450","isPartOf":{"@id":"https://www.guru99.com/c-sharp-file-operations.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/c-sharp-file-operations.html#webpage"}}]}
```
