C#
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented C#
-
Section 4: Working with Data
-
Section 5: Error Handling
-
Section 6: Delegates and Events
-
Section 7: Async Programming
-
Section 8: More Language Features
-
Section 9: File I/O and Serialization
-
Section 10: Networking in .NET
-
Section 11: The .NET Ecosystem
-
Section 12: Memory and Performance
-
Section 13: Concurrency Beyond Async
-
Section 14: Reflection and Attributes
-
Section 15: Testing and Best Practices
-
Section 16: Design Patterns in C#
-
Section 17: Standard Library Deep Dive
-
Section 18: Data Structures and Algorithms in C#
-
Section 19: GUI and Desktop Development Overview
-
Section 20: Practical Projects
-
Section 21: More Practice Exercises
-
Section 22: More Standard Library and Text Processing
-
Section 23: More Design Patterns
-
Section 24: More Projects
-
Section 25: Interview Practice
-
Section 26: C# Keywords Reference (Modifiers)
-
Section 27: C# Keywords Reference (Statements)
-
Section 28: C# Keywords Reference (Operators)
-
Section 29: BCL: System.Collections.Generic
-
Section 30: BCL: System.Linq
-
Section 31: BCL: System.Threading
-
Section 32: BCL: System.IO
-
Section 33: BCL: System.Text and System.Text.Json
-
Section 34: BCL: System.Net.Http
-
Section 35: C# Language Specification Topics
-
Section 36: More Practice Exercises
-
Section 37: More Async Patterns
-
Section 38: More BCL: System.Reflection and System.Diagnostics
-
Section 39: Nullable Reference Types In Depth
-
Section 40: C# Records and Pattern Matching In Depth
-
Section 41: Dependency Injection Deep Dive
-
Section 42: More Interview and Whiteboard Practice
199: File and Directory Static Classes
I once worked with a junior developer who spent an entire afternoon writing a complex recursive function to check if a nested folder structure existed and then manually create each level one by one. He was treating the file system like a puzzle he had to solve with logic. I had to pull him aside and show him Directory.CreateDirectory(). He looked at me like I'd just revealed a magic trick, mainly because he didn't realize that CreateDirectory automatically handles any missing parent folders in the path. It's a common stumbling block; we often assume we need to instantiate "File" or "Directory" objects to work with them, but in C#, the static classes provided by System.IO are designed to do the heavy lifting for you in a single line.
Quick Wins with the File Static Class
The File class is static, which means you never use the new keyword with it. You just call its methods directly. For most of your daily tasks—like saving a config file or reading a log—you won't need to deal with complex FileStream objects. C# gives us "all-in-one" methods that handle opening, writing, and closing the file in one go.
Take File.WriteAllText and File.ReadAllText. These are the "easy buttons" of the IO world. If you have a string and you want it in a file, you use WriteAllText. If you want the whole file back as a string, you use ReadAllText. I usually recommend these for smaller files because they prevent the common mistake of forgetting to close a file handle, which would otherwise lock the file and crash your app the next time you try to access it.
using System.IO;
string filePath = "user_settings.txt";
string content = "Theme=Dark\nFontSize=14\nNotifications=Enabled";
// One line to create the file and write the content
File.WriteAllText(filePath, content);
// One line to pull it all back into a variable
string savedSettings = File.ReadAllText(filePath);
Managing Folders with the Directory Static Class
While the File class handles the actual data, the Directory class manages the containers. Similar to File, this is a static class. You'll spend most of your time here checking if folders exist or grabbing a list of everything inside a folder.
One of the most useful methods here is Directory.GetFiles(). It returns a string array containing the full paths of every file in the specified directory. I've used this countless times to build "batch processors"—where the app looks at a folder, finds every .csv or .jpg file, and processes them in a loop. Just be careful: if you point this at a directory with 100,000 files, you're going to feel that memory hit. For massive directories, there are other methods, but for standard app data, GetFiles is your best friend.
using System.IO;
string folderPath = @"C:\MyApp\Logs";
if (!Directory.Exists(folderPath))
{
// This creates the folder and any missing parents in the path
Directory.CreateDirectory(folderPath);
}
// Get all files in the directory to see what we're dealing with
string[] logFiles = Directory.GetFiles(folderPath);
foreach (string file in logFiles)
{
Console.WriteLine($"Found log file: {Path.GetFileName(file)}");
}
You'll notice I used Path.GetFileName(file) in the loop. Since Directory.GetFiles returns the full absolute path (e.g., C:\MyApp\Logs\log1.txt), using the Path static class allows you to strip away the folder structure and just get the actual filename. It's a small detail, but it makes your console output much cleaner.
📋 Practical Task
Project: Local User Preferences Backup System
Your goal is to create a small utility that simulates backing up user preferences. Write a program that performs the following sequence of operations using the File and Directory static classes:
- Define a folder path called
"UserBackup". - Check if that directory exists; if it doesn't, create it.
- Create a file inside that folder named
"preferences.txt"and write the following text into it:"Language=English, Volume=75, Resolution=1920x1080". - After writing the file, use
Directory.GetFiles()to retrieve all files in the"UserBackup"folder. - Loop through the resulting array and print each filename to the console to verify the backup was successful.
There are no comments for now.