Why is the ability to work with files in C# a superpower?
\n\nImagine you are writing a program that needs to save user settings, log errors, or process thousands of documents. Without file system skills, you will hit a wall. In C#, working with files is not just boring line reading. It is the ability to create powerful utilities, parsers, and even mini-databases.
\n\nIn this article, we will cover everything: from the basic File.ReadAllText to advanced work with streams and folders. You will learn how not to lose data, how to optimize reading large files, and how to navigate the "pitfalls" of the file system. Let's go!
Hero Classes: File, FileInfo, Directory, and Path
\n\nIn .NET, there are two approaches to working with files: static methods of the File class and instance methods of the FileInfo class. The first is for quick "read-write" operations, the second is when you need to work with one file for a long time while maintaining state.
File vs FileInfo: When to use what?
\n\nFile is a static class. You write File.Exists("test.txt") — and it's done. Convenient, but with each operation the system checks the path anew.
FileInfo — you create an object once, and then call methods. This is faster if you are performing multiple actions on one file.
\n\n// Quick way\nif (File.Exists(@"C:\\data\\log.txt"))\n{\n string content = File.ReadAllText(@"C:\\data\\log.txt");\n Console.WriteLine(content);\n}\n\n// Professional way\nFileInfo fileInfo = new FileInfo(@"C:\\data\\log.txt");\nif (fileInfo.Exists)\n{\n using (StreamReader reader = fileInfo.OpenText())\n {\n string content = reader.ReadToEnd();\n Console.WriteLine(content);\n }\n}\n\n\nThe Directory class works on the same principle — static methods for quick operations with folders. Path is an indispensable helper for joining paths: Path.Combine("folder", "file.txt") automatically places the correct slash.
Reading Files: From Small to Gigantic
\n\nReading files in C# is an art of balancing simplicity and performance. Let's break down three levels.
\n\nLevel 1: All at once (for small files)
\n\nIf the file is up to 10 MB, feel free to use ReadAllText or ReadAllLines.
string text = File.ReadAllText("config.json");\nstring[] lines = File.ReadAllLines("users.csv");\n\n\nDownside: with a large file, you risk getting an OutOfMemoryException.
Level 2: Line-by-line reading (for medium files)
\n\nWe use StreamReader — read line by line without loading everything into memory.
using (StreamReader reader = new StreamReader("big_log.txt"))\n{\n string line;\n while ((line = reader.ReadLine()) != null)\n {\n Console.WriteLine(line); // Process each line\n }\n}\n\n\nNote the using statement — it guarantees the file will close even if an error occurs.
Level 3: Reading binary data (for pros)
\n\nWorking with images, archives, or any non-text files requires FileStream.
using (FileStream fs = new FileStream("photo.jpg", FileMode.Open, FileAccess.Read))\n{\n byte[] buffer = new byte[1024];\n int bytesRead;\n while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)\n {\n // Process the chunk of data\n Console.WriteLine($"Read {bytesRead} bytes");\n }\n}\n\n\nWriting Files: How Not to Lose Data
\n\nWriting in C# is not just File.WriteAllText. It is important to understand the modes: create a new file, append to the end, or overwrite.
Basic FileMode values
\n- \n
- CreateNew<