File I/O
Reading and writing files
Introduction
In this lesson, you'll learn about file i/o in C#. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Python, you're familiar with reading and writing files.
C# has its own approach to reading and writing files, which we'll explore step by step.
The C# Way
Let's see how C# handles this concept. Here's a typical example:
using System.IO;
using System.Text.Json;
// Read all
string content = File.ReadAllText("input.txt");
// Write
File.WriteAllText("output.txt", "Hello!");
// Line by line
foreach (string line in File.ReadLines("input.txt")) {
Console.WriteLine(line);
}
// Append
File.AppendAllText("log.txt", "New entry\n");
// Streaming (memory efficient)
using var reader = new StreamReader("input.txt");
while (reader.ReadLine() is string line) {
Console.WriteLine(line);
}
// JSON
var data = JsonSerializer.Deserialize<MyConfig>(
File.ReadAllText("config.json"));Comparing to Python
Here's how you might have written similar code in Python:
from pathlib import Path
# Read all
content = Path("input.txt").read_text()
# Write
Path("output.txt").write_text("Hello!")
# Line by line
with open("input.txt") as f:
for line in f:
print(line.rstrip())
# Append
with open("log.txt", "a") as f:
f.write("New entry\n")
# JSON
import json
data = json.loads(Path("config.json").read_text())
Path("out.json").write_text(json.dumps(data, indent=2))You may be used to different syntax or behavior.
C# File.ReadAllText = Python Path.read_text()
You may be used to different syntax or behavior.
Python 'with open' → C# 'using var reader = new StreamReader'
You may be used to different syntax or behavior.
C# foreach over File.ReadLines = Python 'for line in f'
You may be used to different syntax or behavior.
C# System.Text.Json = Python's json module
Step-by-Step Breakdown
1. Static File Class
C#'s File class provides static methods that mirror Python's pathlib one-liners for common operations.
Path("file.txt").read_text()File.ReadAllText("file.txt")2. Streaming
For large files, use StreamReader with 'using' for automatic cleanup — like Python's 'with open()' pattern.
with open("file.txt") as f:
for line in f: ...using var f = new StreamReader("file.txt");
while (f.ReadLine() is string line) { ... }3. JSON
C#'s System.Text.Json deserializes into typed objects — safer than Python's dict-based json.loads.
data = json.loads(text) # dictvar data = JsonSerializer.Deserialize<Config>(text); // typedCommon Mistakes
When coming from Python, developers often make these mistakes:
- C# File.ReadAllText = Python Path.read_text()
- Python 'with open' → C# 'using var reader = new StreamReader'
- C# foreach over File.ReadLines = Python 'for line in f'
Key Takeaways
- File.ReadAllText/WriteAllText = Path.read_text/write_text
- using StreamReader = with open()
- File.ReadLines for line-by-line iteration
- System.Text.Json for typed JSON deserialization