C# Tutorial: File I/O
Read and write files with System.IO, work with paths, and stream large files.
Files and directories
C#'s `System.IO` namespace provides comprehensive file system access.
`File.ReadAllText` for simple reads. `StreamReader`/`StreamWriter` for large files. Always close streams — use `using` blocks.
What you'll learn in this C# file i/o tutorial
This interactive C# tutorial has 8 hands-on exercises. Estimated time: 18 minutes.
- Read and write text files — `File.WriteAllText` and `File.ReadAllText` are the simplest file operations.
- Read lines — `File.ReadAllLines` returns a string array.
- Append to a file — `File.AppendAllText` adds text to an existing file.
- StreamWriter for large files — Use `StreamWriter` when writing many lines or large data.
- StreamReader for large files — Read large files line by line with `StreamReader`.
- Path class — `Path` helps build and parse file paths in a cross-platform way.
- Directory operations — `Directory` creates and lists directories.
- File.Exists and metadata — Check if a file exists and read its metadata.
C# File I/O concepts covered
- Files and directories