How to Check Whether a Folder Exists with Directory.Exists in C#
Directory.Exists("photos") returns true when there's a folder named photos and false when there isn't. It's the check to run before you list, open or use a folder that may not be there.
The .NET API reference: Determines whether the given path refers to an existing directory on disk. A missing folder doesn't throw an exception; it gives false. The return value, from the same reference: true if path refers to an existing directory; false if the directory does not exist or an error occurs when trying to determine if the specified directory exists.
Syntax
bool <found> = Directory.Exists(<path>);
if (Directory.Exists(<path>))
{
<code that uses the folder>
}
| Part | What it is |
|---|---|
<path> | The folder to look for: a name such as photos, a relative path, or a full path |
Directory.Exists(<path>) | Returns true when the path is an existing folder, and false otherwise |
bool <found> | A variable that keeps the answer |
if (Directory.Exists(<path>)) | Runs the block only when the folder is there |
A name or a relative path doesn't start from the app's own folder. The reference: Relative path information is interpreted as relative to the current working directory.
The Example
A console app called DirectoryExists creates a folder named photos, so there's one to find, and checks for photos and for videos, which doesn't exist. Then it lists the videos folder only if it's there.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n DirectoryExists
cd DirectoryExists
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/DirectoryExists/DirectoryExists.csproj:
Determining projects to restore...
Restored /Users/you/source/DirectoryExists/DirectoryExists.csproj (in 45 ms).
Restore succeeded.
dotnet new console -n DirectoryExists creates a folder named DirectoryExists with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or DirectoryExists.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First, a folder to find:
// Create one folder, so the check has something to find.
Directory.CreateDirectory("photos");
Directory.CreateDirectory("photos") creates a folder named photos in the current folder.
Then the two checks:
// Directory.Exists answers true or false.
Console.WriteLine($"photos exists: {Directory.Exists("photos")}");
Console.WriteLine($"videos exists: {Directory.Exists("videos")}");
Each Directory.Exists call returns a bool, which the string prints as True or False.
Then a check that decides what runs:
// Check first, and list the folder only when it's there.
string folder = "videos";
if (Directory.Exists(folder))
{
Console.WriteLine($"{folder} holds {Directory.GetFiles(folder).Length} files.");
}
else
{
Console.WriteLine($"There's no {folder} folder, so there's nothing to list.");
}
The first block runs only when videos is there. Inside it, Directory.GetFiles(folder) returns the folder's files as an array, and Length counts them. For a missing folder, the else block prints a message instead.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
photos exists: True
videos exists: False
There's no videos folder, so there's nothing to list.

photos exists: True, because the app had just created it, and videos exists: False, because nothing did. With no videos folder, the else block ran, and the app printed a message instead of listing a folder that isn't there.
The Complete File

// Create one folder, so the check has something to find.
Directory.CreateDirectory("photos");
// Directory.Exists answers true or false.
Console.WriteLine($"photos exists: {Directory.Exists("photos")}");
Console.WriteLine($"videos exists: {Directory.Exists("videos")}");
// Check first, and list the folder only when it's there.
string folder = "videos";
if (Directory.Exists(folder))
{
Console.WriteLine($"{folder} holds {Directory.GetFiles(folder).Length} files.");
}
else
{
Console.WriteLine($"There's no {folder} folder, so there's nothing to list.");
}
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Listing a folder without checking first, as in Directory.GetFiles("videos") | The app stopped with Unhandled exception. System.IO.DirectoryNotFoundException: Could not find a part of the path '/Users/you/source/DirectoryExists/videos'. | Check with Directory.Exists first, as the example does |
| Passing the path of a file | With a file named notes.txt there, a check printed Directory.Exists("notes.txt"): False and File.Exists("notes.txt"): True. Directory.Exists only answers for folders | Use File.Exists for a file |
| Running the app from another folder | photos is a relative path, so it's looked up in the current folder. Run from /Users/you/source/DirectoryExists, a check printed photos exists: True. Run from /Users/you/source, the same check printed photos exists: False | Start the app from the folder your relative paths assume, or pass a full path |
FAQ
Is Directory.Exists case-sensitive?
It follows the file system. The reference: The case-sensitivity of the path parameter corresponds to that of the file system on which the code is running. For example, it's case-insensitive on NTFS (the default Windows file system) and case-sensitive on Linux file systems. On the Mac these runs used, with the folder named photos in lowercase, a check printed photos: True and PHOTOS: True.
What does it return for an empty or null path?
false, with no exception. A check printed Empty: False, null: False, and Contains a null character: False for a path with a null character in it. The reference: The Exists method returns false if any error occurs while trying to determine if the specified file exists.
Conclusion
Call Directory.Exists(path) before you list, open or use a folder that may not be there. It returns true only for an existing folder, and false for a missing folder, a file, or a path it can't check. A name or a relative path is looked up in the current folder.
Additional Resources
- Directory.Exists Method — what it returns, relative paths and letter case.
- File.Exists Method — the same check for a file.
- Directory.GetFiles Method — listing the files in a folder.
