In C#, Directory is a static class in the System.IO namespace. A directory is just a folder. The class gives you ready-made methods to make a folder with CreateDirectory(), check it with Exists(), list its contents with GetFiles(), and remove it with Delete().
Because it is static, you never write new Directory(). You call Directory.Method(path) and pass the folder path each time. Add using System.IO; at the top of your file to use it.
Methods such as Delete(path, true) remove a whole folder with no undo. Every example here builds a demo folder inside Path.GetTempPath() and deletes only that folder. Do the same while you learn.
Syntax
C# Syntax
Common first steps:
C#
using System.IO;
string folder = Path.Combine(Path.GetTempPath(), "codetofun-demo");
Directory.CreateDirectory(folder); // make it (no error if it exists)
bool there = Directory.Exists(folder); // true or false
string[] files = Directory.GetFiles(folder); // files as an array
string[] subs = Directory.GetDirectories(folder); // subfolders as an array
Directory.Delete(folder); // remove it (must be empty)
Reference
Directory vs DirectoryInfo vs Path
Three classes in System.IO work with folder paths. Pick the one that matches the job:
Class
What it is
Good for
Directory
A static class. You pass the path to each method.
Quick one-off jobs: create, check, list, delete
DirectoryInfo
An object that remembers one folder.
Many actions on the same folder, plus properties such as Name and Parent
Path
A static class that works only with text.
Joining, splitting, and reading parts of a path. It never touches the disk
Example 1 — Build a Small Folder Tree, List It, and Delete It
A first program that works only inside a temp demo folder: create two subfolders and a file, list them in sorted order, then clean up.
C#
using System;
using System.Collections.Generic;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-hub-demo");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(Path.Combine(root, "docs"));
Directory.CreateDirectory(Path.Combine(root, "images"));
File.WriteAllText(Path.Combine(root, "readme.txt"), "hello");
List<string> names = new List<string>();
foreach (string entry in Directory.EnumerateFileSystemEntries(root)) {
names.Add(Path.GetFileName(entry));
}
names.Sort(StringComparer.Ordinal);
foreach (string name in names) {
Console.WriteLine(name);
}
Directory.Delete(root, true);
Console.WriteLine("Exists after delete: " + Directory.Exists(root));
}
}
Output
docs
images
readme.txt
Exists after delete: False
How It Works
A private demo path.Path.GetTempPath() plus a fixed name gives a folder that belongs to this lesson. The first if removes a leftover from an earlier run.
Build the tree.CreateDirectory makes the two subfolders, and File.WriteAllText adds readme.txt to the root.
List, then sort.EnumerateFileSystemEntries gives files and folders in any order, so the names go into a List that is sorted with StringComparer.Ordinal.
Clean up.Delete(root, true) removes everything the program made, and Exists confirms it with False.
Example 2 — Directory (Static) vs DirectoryInfo (Object)
Do the same job twice: once with the static Directory class and once with a DirectoryInfo object that remembers the folder.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-hub-compare");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
Console.WriteLine(Directory.Exists(root));
Console.WriteLine(Directory.GetFiles(root).Length);
DirectoryInfo info = new DirectoryInfo(root);
Console.WriteLine(info.Name);
File.WriteAllText(Path.Combine(root, "a.txt"), "hi");
Console.WriteLine(info.GetFiles().Length);
info.Delete(true);
Console.WriteLine(Directory.Exists(root));
}
}
Output
True
0
codetofun-hub-compare
1
False
How It Works
Static style.Directory.Exists(root) and Directory.GetFiles(root) pass the path every time. There is no object to create.
Object style.new DirectoryInfo(root) stores the path once. After that, info.Name and info.GetFiles() need no argument.
Both see the same disk. After a.txt is written, info.GetFiles().Length is 1, and the folder is the same one the static calls looked at.
Delete from either side.info.Delete(true) removes the folder with its contents, and the static Directory.Exists prints False. For one or two quick calls, use Directory. For many calls on one folder, DirectoryInfo reads better.
Example 3 — Path Builds the Text, Directory Touches the Disk
Build a file path with Path first, see that nothing exists yet, then let Directory and File make it real.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-hub-path");
if (Directory.Exists(root)) Directory.Delete(root, true);
string report = Path.Combine(root, "reports", "2026.txt");
Console.WriteLine(Path.GetFileName(report));
Console.WriteLine(Path.GetFileName(Path.GetDirectoryName(report)));
Console.WriteLine(Directory.Exists(root));
Directory.CreateDirectory(Path.GetDirectoryName(report));
File.WriteAllText(report, "ok");
Console.WriteLine(Directory.Exists(root));
Console.WriteLine(File.Exists(report));
Directory.Delete(root, true);
}
}
Output
2026.txt
reports
False
True
True
How It Works
Only text so far.Path.Combine joins the pieces with the right separator for your system. Nothing is created yet.
Take the text apart.GetFileName gives 2026.txt, and GetDirectoryName then GetFileName gives reports. Both work without a real file.
The disk is still empty.Directory.Exists(root) prints False, which proves that a Path method never touches the disk.
Directory makes it real.CreateDirectory builds root and reports in one call, and File.WriteAllText adds the file. The final checks print True.
Continue
Next: C# Directory Methods Index
After you know what the Directory class is for, open the method index for a full tutorial on each Directory API.
Directory is a static class in System.IO. It has methods to create, delete, move, check, and list folders, and to read the working directory, the parent of a path, the root of a path, and the drives of the computer.
Directory is static: you pass the path each time. DirectoryInfo is an object created with new DirectoryInfo(path) that remembers one folder and gives properties such as Name, Parent, and Exists.
Path only works with text: it joins, splits, and reads parts of a path, and never touches the disk. Directory talks to the disk: it creates, deletes, and lists real folders.
Yes. They use a demo folder inside Path.GetTempPath(), create everything first, and delete only what they made. Never copy the recursive Delete(path, true) call onto a real folder.
Open the C# Directory Methods index. It lists CreateDirectory, Delete, Move, Exists, the Get and Enumerate list methods, GetCurrentDirectory, SetCurrentDirectory, GetDirectoryRoot, GetParent, and GetLogicalDrives.