Working with the File System in C#: Practical Examples for .NET

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with the File System in C#: From Theory to Practice



Working with the file system is one of the basic tasks every developer encounters. Whether it's logging, processing configuration files, importing data, or generating reports — the ability to properly read, write, and manage files in C# (.NET) is essential in any project. In this article, we will explore practical examples of working with the file system in C#, starting from simple operations and moving on to directory handling.



We will use the System.IO namespace, which provides all the necessary classes for interacting with the file system.



1. Main Classes for Working with Files and Folders



Before writing code, it's important to get acquainted with the key classes:



  • File — a static class for creating, copying, deleting, and moving files.
  • FileInfo — an instance class that provides information about a specific file and methods for working with it.
  • Directory — a static class for working with folders.
  • DirectoryInfo — an instance class for obtaining information about a folder.
  • Path — a utility class for working with file paths.


Let's look at a simple example — checking if a file exists and creating it:



using System;using System.IO;

class Program{ static void Main() { string filePath = @"C:\\Test\\example.txt";

if (File.Exists(filePath)) { Console.WriteLine("File exists."); } else { File.Create(filePath).Close(); Console.WriteLine("File created."); } }}


Note: after File.Create(), we call Close() to release the stream. In modern projects, it's better to use the using construct.



2. Reading and Writing Text Files



The most common scenario is reading and writing text information. In C#, there are convenient classes StreamReader and StreamWriter for this.



2.1 Writing Data to a File



Example of writing strings to a file using StreamWriter:



string path = @"C:\\Test\\log.txt";string[] lines = { "First line", "Second line", "Third line" };

using (StreamWriter writer = new StreamWriter(path, false)){ foreach (string line in lines) { writer.WriteLine(line); }}Console.WriteLine("Data written.");


The second parameter of the StreamWriter constructor — false — means overwriting the file. If you specify true, data will be appended to the end.



2.2 Reading Data from a File



Reading the entire content or line by line:



string path = @"C:\\Test\\log.txt";

// Reading the entire text as a single stringstring allText = File.ReadAllText(path);Console.WriteLine("All text:" + allText);

// Reading line by linestring[] allLines = File.ReadAllLines(path);Console.WriteLine("Line by line:");foreach (string line in allLines){ Console.WriteLine(line);}


For large files, it's better to use StreamReader with line-by-line reading in a loop to avoid loading the entire file into memory.



using (StreamReader reader = new StreamReader(path)){    string currentLine;    while ((currentLine = reader.ReadLine()) != null)    {        Console.WriteLine(currentLine);    }}


3. Working with Binary Files (FileStream)



When you need to work with binary data (images, archives, serialized objects), the FileStream class is used. It allows reading and writing data as bytes.



Example of writing and reading a byte array:



Blogs

Book Recommendations