In C#, Directory.Exists() answers one question: is there a folder at this path? It returns true or false.
The method does not throw. A path that does not exist, a path that points to a file, a null or empty path, an invalid path, and a path you are not allowed to see all give false.
This is a static method of System.IO.Directory. Call it as Directory.Exists(...); there is no new Directory(). For an object that remembers one folder, see DirectoryInfo.
Syntax
Pass the path you want to test. Use File.Exists for files.
C#
string folder = Path.Combine(Path.GetTempPath(), "codetofun-demo");
if (Directory.Exists(folder)) {
Console.WriteLine("Folder is there");
}
Call
What it does
Directory.Exists(path)
true only if a folder is found at path.
Directory.Exists(null)
false. It does not throw for a null or empty path.
File.Exists(path)
The matching check for files. A folder path gives false here.
The result is only a snapshot. Another program can create or delete the folder one moment later, so do not rely on Exists alone for safety. Often CreateDirectory (safe to repeat) is simpler.
Library declarations
C#
public static bool Exists(string? path);
Parameters
Parameter
Description
path
The folder path to test. It may be null, empty, relative, or absolute.
Return Value
Return Value
Description
bool
true if the path names an existing folder. false for a missing folder, a file, an invalid path, or when access is denied.
C# Examples
Example 1 — Check a Folder Before and After Creating It
The same path gives False, then True, then False again.
C#
using System;
using System.IO;
class Program {
static void Main() {
string folder = Path.Combine(Path.GetTempPath(), "codetofun-exists-basic");
if (Directory.Exists(folder)) Directory.Delete(folder, true);
Console.WriteLine("Before: " + Directory.Exists(folder));
Directory.CreateDirectory(folder);
Console.WriteLine("Created: " + Directory.Exists(folder));
Directory.Delete(folder);
Console.WriteLine("Deleted: " + Directory.Exists(folder));
}
}
Output
Before: False
Created: True
Deleted: False
How It Works
Same path, three moments. The string folder never changes. Only the disk changes between the calls.
Before. The setup line removed any old copy, so the first answer is False.
After CreateDirectory. Now a folder sits at that path, and Exists says True.
After Delete. Each Exists call looks at the disk again, so it reports False when the folder is gone.
Example 2 — Exists Is False for a File
Point Directory.Exists at a file and compare it with File.Exists.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-exists-file");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string file = Path.Combine(root, "data.txt");
File.WriteAllText(file, "hello");
Console.WriteLine("Directory.Exists(file): " + Directory.Exists(file));
Console.WriteLine("File.Exists(file): " + File.Exists(file));
Console.WriteLine("Directory.Exists(root): " + Directory.Exists(root));
Console.WriteLine("File.Exists(root): " + File.Exists(root));
Directory.Delete(root, true);
}
}
Two kinds of entries. The demo has one folder (root) and one file (data.txt) inside it.
The file fails the folder test. A file is real, but it is not a directory, so Directory.Exists(file) is False.
The folder fails the file test. The mirror image: File.Exists(root) is False because root is a folder.
Pick the right one. Use Directory.Exists for folders and File.Exists for files. Neither throws, so a wrong choice just returns false.
Example 3 — Odd Inputs Never Throw
Pass null and an empty string, and also add a trailing separator to a real path.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-exists-edge");
Directory.CreateDirectory(root);
string withSlash = root + Path.DirectorySeparatorChar;
Console.WriteLine(Directory.Exists(null));
Console.WriteLine(Directory.Exists(""));
Console.WriteLine(Directory.Exists(withSlash));
Console.WriteLine(Directory.Exists(Path.Combine(root, "nope")));
Directory.Delete(root, true);
}
}
Output
False
False
True
False
How It Works
null is allowed. The parameter accepts null and quietly returns False, which is different from methods such as CreateDirectory that throw.
Empty is the same. An empty string is not a valid folder path, so the answer is again False.
A trailing separator is fine.Path.DirectorySeparatorChar is \ on Windows and / on Linux and macOS. The folder is still found, so the answer is True.
A missing child.nope was never created, so the last line is False. Use this pattern to decide whether to create a folder.
Frequently Asked Questions
It returns false. Directory.Exists is true only for folders. Use File.Exists to test a file.
No. It returns false for null, an empty string, an invalid path, and also when you do not have permission to see the folder.
Usually not. CreateDirectory does nothing when the folder is already there, so you can call it directly.
It is only true at that moment. Another program can create or delete the folder just after your check, so handle exceptions in the code that uses the folder.
Yes. A relative path is checked against the current directory. The answer then depends on where the program runs, so absolute paths are clearer.