Working with the File System in C#: For Beginners
Working with the file system is one of the key skills for any developer. C# (.NET) provides powerful tools for creating, reading, writing, and deleting files and directories. In this article, we will cover basic file system operations that will be useful for both beginners and experienced programmers. All examples are written in C# and use the System.IO namespace.
Basics of Working with the System.IO Namespace
The System.IO namespace contains all the main classes for interacting with the file system. To get started, add the following directive at the beginning of your file:
using System.IO;using System; // for Console and other basic operationsThe main classes we will use are:
File— static methods for working with files (creating, copying, deleting).FileInfo— an instance class for getting information about a specific file.Directory— static methods for working with folders.DirectoryInfo— an instance class for information about a directory.Path— utilities for working with paths.
Let's look at each aspect in practice.
Creating and Writing to a File
The simplest way to create a file and write text to it is to use the File.WriteAllText method. It creates a file (or overwrites an existing one) and writes a string.
string path = @"C:\\Temp\\example.txt";string content = "Hello, world! This is a test file.";
File.WriteAllText(path, content);Console.WriteLine("File created and written.");If you need to write multiple lines, use File.WriteAllLines:
string[] lines = { "First line", "Second line", "Third line" };File.WriteAllLines(path, lines);To append data to the end of an existing file, use File.AppendAllText:
File.AppendAllText(path, "Appended line.");Reading Data from a File
Reading a file is also intuitive. The File.ReadAllText method returns the entire content as a single string:
string readContent = File.ReadAllText(path);Console.WriteLine("File content:");Console.WriteLine(readContent);If the file consists of lines, it is more convenient to use File.ReadAllLines, which returns an array of strings:
string[] readLines = File.ReadAllLines(path);foreach (string line in readLines){ Console.WriteLine(line);}For large files, it is better to use streaming reading with StreamReader to avoid loading the entire file into memory:
using (StreamReader reader = new StreamReader(path)){ string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); }}Working with Folders (Directories)
Creating, checking existence, and deleting folders are common tasks. Here is how to do it using the Directory class:
string dirPath = @"C:\\Temp\\MyFolder";
// Checking existenceif (!Directory.Exists(dirPath)){ Directory.CreateDirectory(dirPath); Console.WriteLine("Folder created.");}else{ Console.WriteLine("Folder already exists.");}
// Deleting a folder (if it is empty)if (Directory.Exists(dirPath)){ Directory.Delete(dirPath); Console.WriteLine("Folder deleted.");}To get a list of files in a folder, use Directory.GetFiles:
string[] files = Directory.GetFiles(@"C:\\Temp");foreach (string file in files){ Console.WriteLine(file);}Similarly, for sub