By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
File and directory classes in C# are fundamental for managing files and directories within an application. Understanding how to read, write, delete, and check the existence of files and directories is crucial for data management, application functionality, and system integrity. Incorrect handling can lead to data loss, application crashes, or security vulnerabilities. For example, improper file handling can result in corrupted data or unauthorized access, compromising the entire system.
File.ReadAllText()
string content = File.ReadAllText("path/to/file.txt");
⚠️ Pitfall: Ensure the file path is correct to avoid FileNotFoundException.
FileNotFoundException
Writing to a File
File.WriteAllText()
File.WriteAllText("path/to/file.txt", "Hello, World!");
⚠️ Pitfall: Overwrites the file if it already exists. Use File.AppendAllText() to append.
File.AppendAllText()
Deleting a File
File.Delete()
File.Delete("path/to/file.txt");
⚠️ Pitfall: Ensure the file is not in use to avoid IOException.
IOException
Checking File Existence
File.Exists()
bool fileExists = File.Exists("path/to/file.txt");
⚠️ Pitfall: Do not rely solely on this check; the file state can change between checks.
Creating a Directory
Directory.CreateDirectory()
Directory.CreateDirectory("path/to/directory");
⚠️ Pitfall: Ensure the path is valid to avoid UnauthorizedAccessException.
UnauthorizedAccessException
Deleting a Directory
Directory.Delete()
Directory.Delete("path/to/directory");
⚠️ Pitfall: Ensure the directory is empty or use Directory.Delete("path/to/directory", true); to delete recursively.
Directory.Delete("path/to/directory", true);
Checking Directory Existence
Directory.Exists()
bool directoryExists = Directory.Exists("path/to/directory");
Experts view file and directory operations as a series of atomic actions that must be carefully orchestrated to maintain data integrity and system stability. They think in terms of error handling, concurrency, and security, always considering the potential for race conditions and unauthorized access.
Exam trap: Questions may include scenarios where exceptions are not handled.
The mistake: Relying solely on File.Exists() or Directory.Exists().
Exam trap: Scenarios where the file state changes between checks.
The mistake: Not checking file permissions.
Exam trap: Questions involving permission issues.
The mistake: Overwriting files unintentionally.
Exam trap: Scenarios where data is accidentally overwritten.
The mistake: Not handling concurrent file access.
File.AppendAllText("log.txt", "New log entry");
Why it works: AppendAllText adds new content without overwriting existing data.
AppendAllText
Scenario: A temporary file needs to be deleted after processing.
csharp try { File.Delete("temp.txt"); } catch (IOException ex) { Console.WriteLine("Error deleting file: " + ex.Message); }
Why it works: Proper exception handling prevents application crashes.
Scenario: A directory needs to be created if it does not exist.
CreateDirectory
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.