Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
DirectoryNotFoundException in C#
The DirectoryNotFoundException in C# is thrown when an application attempts to access a directory that does not exist on the file system. This exception is part of the System.IO namespace and commonly occurs during file and directory operations.
Syntax
The exception is automatically thrown by the .NET framework when directory operations fail −
public class DirectoryNotFoundException : SystemException
Common methods that can throw this exception include −
Directory.GetDirectories(path); Directory.GetFiles(path); DirectoryInfo directoryInfo = new DirectoryInfo(path);
When DirectoryNotFoundException Occurs
Here is an example that demonstrates when this exception occurs by trying to access a non-existent directory −
using System;
using System.IO;
class Program {
static void Main() {
try {
Directory.GetDirectories("D:\NonExistentDirectory");
}
catch (DirectoryNotFoundException ex) {
Console.WriteLine("Exception caught: " + ex.GetType().Name);
Console.WriteLine("Message: " + ex.Message);
}
}
}
The output of the above code is −
Exception caught: DirectoryNotFoundException Message: Could not find a part of the path 'D:\NonExistentDirectory'.
Handling DirectoryNotFoundException
The best practice is to check if a directory exists before performing operations on it using Directory.Exists() −
using System;
using System.IO;
class Program {
static void Main() {
string path = "D:\TestDirectory";
if (Directory.Exists(path)) {
string[] directories = Directory.GetDirectories(path);
Console.WriteLine("Found " + directories.Length + " directories");
}
else {
Console.WriteLine("Directory does not exist: " + path);
Console.WriteLine("Creating the directory...");
Directory.CreateDirectory(path);
Console.WriteLine("Directory created successfully");
}
}
}
The output of the above code is −
Directory does not exist: D:\TestDirectory\ Creating the directory... Directory created successfully
Using Try-Catch with Multiple Exceptions
When working with directory operations, it's good practice to handle multiple possible exceptions −
using System;
using System.IO;
class Program {
static void Main() {
string path = "C:\InvalidPath<>";
try {
string[] files = Directory.GetFiles(path);
Console.WriteLine("Files found: " + files.Length);
}
catch (DirectoryNotFoundException) {
Console.WriteLine("Directory not found");
}
catch (ArgumentException) {
Console.WriteLine("Invalid path format");
}
catch (UnauthorizedAccessException) {
Console.WriteLine("Access denied");
}
catch (Exception ex) {
Console.WriteLine("General error: " + ex.Message);
}
}
}
The output of the above code is −
Invalid path format
Common Scenarios
| Operation | Description |
|---|---|
| Directory.GetDirectories() | Thrown when the specified directory path does not exist |
| Directory.GetFiles() | Thrown when attempting to list files in a non-existent directory |
| DirectoryInfo constructor | May throw when accessing properties of non-existent directories |
| File operations | Thrown when trying to create files in non-existent directories |
Conclusion
The DirectoryNotFoundException occurs when trying to access directories that don't exist. Always use Directory.Exists() to check directory existence before operations, or implement proper exception handling with try-catch blocks to gracefully handle missing directories.
