codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

30 / 60 topics
30File I/O Operations in C#31Streaming in C#32Serialization in C#
Tutorials/C# Programming/File I/O Operations in C#
🔷C# Programming

File I/O Operations in C#

Updated 2026-05-15
10 min read

File I/O Operations in C#

Introduction

In the world of programming, file input/output (I/O) operations are fundamental. They allow your applications to interact with files on the disk, enabling you to read data from files and write data to them. This tutorial will guide you through the basics of file handling in C#, covering both reading from and writing to files.

Concept

C# provides several classes within the System.IO namespace to handle file I/O operations. The most commonly used classes for basic file operations are StreamReader, StreamWriter, File, and FileInfo.

  • StreamReader: Used for reading characters from a byte stream in a particular encoding.
  • StreamWriter: Used for writing characters to a byte stream in a particular encoding.
  • File: Provides static methods for the creation, copying, deletion, moving, and opening of files and directories.
  • FileInfo: Provides properties and instance methods for the creation, copying, deletion, moving, and opening of files.

Examples

Reading from a File

To read text from a file, you can use StreamReader. Here's a simple example:

csharp
1using System;
2using System.IO;
3
4class Program
5{
6 static void Main()
7 {
8 string filePath = "example.txt";
9
10 // Check if the file exists
11 if (File.Exists(filePath))
12 {
13 using (StreamReader reader = new StreamReader(filePath))
14 {
15 string content = reader.ReadToEnd();
16 Console.WriteLine(content);
17 }
18 }
19 else
20 {
21 Console.WriteLine("File does not exist.");
22 }
23 }
24}

In this example, we first check if the file exists to avoid exceptions. If it does, we create a StreamReader object and read all the content of the file using ReadToEnd(). The using statement ensures that the StreamReader is properly disposed of after use.

Writing to a File

To write text to a file, you can use StreamWriter. Here's how you can do it:

csharp
1using System;
2using System.IO;
3
4class Program
5{
6 static void Main()
7 {
8 string filePath = "example.txt";
9 string contentToWrite = "Hello, world!";
10
11 using (StreamWriter writer = new StreamWriter(filePath))
12 {
13 writer.WriteLine(contentToWrite);
14 }
15
16 Console.WriteLine("Content written to file.");
17 }
18}

In this example, we create a StreamWriter object and write a line of text to the file. The using statement ensures that the StreamWriter is properly disposed of after use.

Appending to a File

If you want to append text to an existing file instead of overwriting it, you can open the file with the FileMode.Append mode:

csharp
1using System;
2using System.IO;
3
4class Program
5{
6 static void Main()
7 {
8 string filePath = "example.txt";
9 string contentToAppend = "This is an appended line.";
10
11 using (StreamWriter writer = new StreamWriter(filePath, true))
12 {
13 writer.WriteLine(contentToAppend);
14 }
15
16 Console.WriteLine("Content appended to file.");
17 }
18}

Here, the StreamWriter constructor's second parameter is set to true, which opens the file in append mode.

Using File and FileInfo

C# also provides static methods in the File class for reading and writing files. Here are some examples:

Reading All Text from a File

csharp
1using System;
2using System.IO;
3
4class Program
5{
6 static void Main()
7 {
8 string filePath = "example.txt";
9
10 if (File.Exists(filePath))
11 {
12 string content = File.ReadAllText(filePath);
13 Console.WriteLine(content);
14 }
15 else
16 {
17 Console.WriteLine("File does not exist.");
18 }
19 }
20}

Writing All Text to a File

csharp
1using System;
2using System.IO;
3
4class Program
5{
6 static void Main()
7 {
8 string filePath = "example.txt";
9 string contentToWrite = "Hello, world!";
10
11 File.WriteAllText(filePath, contentToWrite);
12
13 Console.WriteLine("Content written to file.");
14 }
15}

These methods are simpler and more concise for basic operations.

What's Next?

In the next section, we will explore streaming in C#, which is useful for handling large files or streams of data efficiently. Stay tuned!

Info

Always ensure that you handle exceptions properly when dealing with file I/O operations to avoid runtime errors.


PreviousCustom Exceptions in C#Next Streaming in C#

Recommended Gear

Custom Exceptions in C#Streaming in C#