Directory.Exists() / Directory.CreateDirectory()
How to use the Directory class methods for working with directories — checking existence, creating, deleting, and listing files.
Syntax
using System.IO; // Check whether a directory exists. bool exists = Directory.Exists(string path); // Create a directory (also creates any intermediate directories recursively). Directory.CreateDirectory(string path); // Delete an empty directory. Directory.Delete(string path); // Delete a directory recursively (including all its contents). Directory.Delete(string path, bool recursive); // Get a list of file paths in a directory. string[] files = Directory.GetFiles(string path); string[] files = Directory.GetFiles(string path, string searchPattern); // Get a list of subdirectory paths in a directory. string[] dirs = Directory.GetDirectories(string path); // Move (or rename) a directory. Directory.Move(string sourceDirName, string destDirName);
Method List
| Method | Description |
|---|---|
| Directory.Exists(path) | Returns true if a directory exists at the specified path. |
| Directory.CreateDirectory(path) | Creates the directory. Also creates any missing intermediate directories. Does nothing if the directory already exists. |
| Directory.Delete(path) | Deletes an empty directory. Throws an IOException if the directory contains any files or subdirectories. |
| Directory.Delete(path, true) | Deletes the directory and all of its contents recursively. |
| Directory.GetFiles(path) | Returns the full paths of all files in the directory as a string array. |
| Directory.GetFiles(path, pattern) | Returns a filtered list of files matching a pattern such as "*.txt". |
| Directory.GetDirectories(path) | Returns the full paths of all subdirectories in the directory. |
| Directory.Move(src, dest) | Moves a directory to a new location. Can also be used to rename a directory. |
Sample Code
using System;
using System.IO;
string basePath = "work";
string subPath = Path.Combine(basePath, "logs", "2024");
// Use Directory.Exists() to check whether the directory exists.
Console.WriteLine(Directory.Exists(basePath)); // False
// Use Directory.CreateDirectory() to create the directory.
// Intermediate paths (logs/2024) are also created in one call.
Directory.CreateDirectory(subPath);
Console.WriteLine(Directory.Exists(subPath)); // True
// Create some test files.
File.WriteAllText(Path.Combine(subPath, "app.log"), "app log");
File.WriteAllText(Path.Combine(subPath, "error.log"), "error log");
File.WriteAllText(Path.Combine(subPath, "data.txt"), "data file");
// Use Directory.GetFiles() to list all files.
Console.WriteLine("--- All files ---");
foreach (string file in Directory.GetFiles(subPath))
{
Console.WriteLine(Path.GetFileName(file));
}
// Filter by pattern (*.log only).
Console.WriteLine("--- .log files only ---");
foreach (string file in Directory.GetFiles(subPath, "*.log"))
{
Console.WriteLine(Path.GetFileName(file));
}
// Use Directory.GetDirectories() to list subdirectories.
Console.WriteLine("--- Subdirectories ---");
foreach (string dir in Directory.GetDirectories(basePath))
{
Console.WriteLine(Path.GetFileName(dir));
}
// Use Directory.Delete() to recursively delete the directory and its contents.
Directory.Delete(basePath, recursive: true);
Console.WriteLine($"After deletion: {Directory.Exists(basePath)}"); // False
Notes
Directory.CreateDirectory() does not throw an exception if the directory already exists, so you can call it safely without checking with Exists() first. Directory.Delete(path, true) permanently deletes the directory and all its contents with no way to undo — verify the path carefully before calling it.
Use Path.Combine() / Path.GetFileName() to build path strings, as it handles path separator differences across operating systems. For reading and writing files, see File.ReadAllText() / WriteAllText().
If you find any errors or copyright issues, please contact us.