Reading and Writing Files
Learn how to create, read, and write files using C#.
Reading and Writing Files is a free C# Academy lesson on CoddyKit — lesson 1 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Reading and Writing Files
Welcome to the first lesson in the "Working with Files and Data" category! In this lesson, you’ll learn how to read and write files in C#, enabling you to store and retrieve data in text files. Let’s get started!

2
Why Work with Files?
Files allow programs to persist data, making it available even after the application is closed. Whether storing user data, logs, or configurations, file handling is a critical skill for developers.
Key Point: C# provides built-in classes like File, StreamReader, and StreamWriter to handle file operations.
3
Reading Files
To read the contents of a file, use the File.ReadAllText() method for small files or StreamReader for larger files. Example:
Key Point: Always check if the file exists before trying to read it.
using System;
using System.IO;
class Program {
static void Main() {
string filePath = "example.txt";
// Reading a file using File.ReadAllText()
if (File.Exists(filePath)) {
string content = File.ReadAllText(filePath);
Console.WriteLine("File Content:");
Console.WriteLine(content);
} else {
Console.WriteLine("File does not exist.");
}
}
}
4
Writing Files
To write data to a file, use the File.WriteAllText() method for small files or StreamWriter for more control. Example:
Tip: Use File.AppendAllText() to add data to an existing file without overwriting it.
using System;
using System.IO;
class Program {
static void Main() {
string filePath = "output.txt";
string content = "Hello, this is a test file.";
// Writing to a file using File.WriteAllText()
File.WriteAllText(filePath, content);
Console.WriteLine("File written successfully.");
}
}
5
Reading Files Line by Line
For large files, it’s more efficient to read them line by line using StreamReader. Example:
Key Point: Use the using statement to ensure the file is properly closed after reading.
using System;
using System.IO;
class Program {
static void Main() {
string filePath = "example.txt";
// Reading a file line by line using StreamReader
if (File.Exists(filePath)) {
using (StreamReader reader = new StreamReader(filePath)) {
string line;
while ((line = reader.ReadLine()) != null) {
Console.WriteLine(line);
}
}
} else {
Console.WriteLine("File does not exist.");
}
}
}
6
Appending Data to Files
To add data to an existing file, use File.AppendAllText() or StreamWriter with the append flag set to true. Example:
Tip: Use Environment.NewLine to ensure proper line breaks.
using System;
using System.IO;
class Program {
static void Main() {
string filePath = "output.txt";
string newContent = "This line will be appended.";
// Appending data to a file
File.AppendAllText(filePath, newContent + Environment.NewLine);
Console.WriteLine("Data appended successfully.");
}
}
7
Practice Challenge
Create a program that writes a list of user-entered names to a file and then reads the file to display the names. Example:
using System;
using System.IO;
class Program {
static void Main() {
string filePath = "names.txt";
// Writing user-entered names to a file
Console.WriteLine("Enter names (type 'exit' to stop):");
using (StreamWriter writer = new StreamWriter(filePath)) {
string name;
while ((name = Console.ReadLine()) != "exit") {
writer.WriteLine(name);
}
}
// Reading the file
Console.WriteLine("\nNames in the file:");
using (StreamReader reader = new StreamReader(filePath)) {
string line;
while ((line = reader.ReadLine()) != null) {
Console.WriteLine(line);
}
}
}
}
8
9
Best Practices for File Handling
Here are some best practices:
- Always check if a file exists before reading or appending to it.
- Use the
usingstatement to automatically close file streams. - Handle exceptions to avoid crashes when dealing with missing or locked files.
Tip: Proper file handling ensures your program is robust and error-free.
10
Great Job!
Congratulations! You’ve learned how to read and write files in C#. In the next lesson, we’ll explore working with JSON and XML data formats. Let’s keep coding!

Frequently asked questions
Is the “Reading and Writing Files” lesson free?
Yes — the full text of “Reading and Writing Files” is free to read here on the web, and the C# Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Reading and Writing Files”?
Learn how to create, read, and write files using C#. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “Reading and Writing Files” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.