In C#, Directory.CreateDirectory() makes a new folder on disk. If the path has parents that do not exist yet, such as reports\2026\march, it creates every missing level for you.
The method does nothing when the folder already exists, so it is safe to call before every save. It always returns a DirectoryInfo object that describes the folder, whether it was new or already there.
This is a static method of System.IO.Directory. Call it as Directory.CreateDirectory(...); there is no new Directory(). For an object that remembers one folder, see DirectoryInfo.
Syntax
Pass the path of the folder you want. Build the path with Path.Combine so it works on Windows, Linux, and macOS.
C#
string folder = Path.Combine(Path.GetTempPath(), "codetofun-demo");
DirectoryInfo info = Directory.CreateDirectory(folder);
Console.WriteLine(info.Name);
Call
What it does
Directory.CreateDirectory(path)
Creates the folder and any missing parents. Returns a DirectoryInfo.
Directory.CreateDirectory(path, mode)
Same, but sets Unix permissions with a UnixFileMode (.NET 7 and later). Windows ignores the mode.
Directory.CreateDirectory("a/b/c")
Builds a, then b, then c. Missing levels are never an error.
Directory is a static class, so you never write new Directory(). It does the work straight away. DirectoryInfo is the object version that remembers one folder.
Library declarations
C#
public static DirectoryInfo CreateDirectory(string path);
public static DirectoryInfo CreateDirectory(string path, UnixFileMode unixCreateMode);
Parameters
Parameter
Description
path
The folder to create. It can be absolute or relative to the current directory. It must not be empty or only spaces.
unixCreateMode
Optional. Unix permission bits for the new folder (.NET 7+). Ignored on Windows.
Return Value
Return Value
Description
DirectoryInfo
An object for the folder. Use Name, FullName, or Exists on it. It is returned even when the folder was already there.
C# Examples
Example 1 — Create a Folder and Check It
Make one demo folder in the temp directory, confirm it exists, then remove it.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-create-demo");
if (Directory.Exists(root)) Directory.Delete(root, true);
DirectoryInfo info = Directory.CreateDirectory(root);
Console.WriteLine(info.Name);
Console.WriteLine(Directory.Exists(root));
Directory.Delete(root);
Console.WriteLine(Directory.Exists(root));
}
}
Output
codetofun-create-demo
True
False
How It Works
A private demo path.Path.GetTempPath() plus a fixed name keeps the experiment away from your real folders. The first if clears any leftover from an earlier run.
The returned object.CreateDirectory hands back a DirectoryInfo, and info.Name is only the last part of the path, so the output is the same on every machine.
Proof it is on disk.Directory.Exists(root) prints True right after creation.
Clean up. The folder is empty, so a plain Delete(root) works, and the last check prints False.
Example 2 — Create Nested Folders in One Call
Ask for three levels at once. The parent levels do not exist yet.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-create-nested");
if (Directory.Exists(root)) Directory.Delete(root, true);
string deep = Path.Combine(root, "a", "b", "c");
Directory.CreateDirectory(deep);
Console.WriteLine(Directory.Exists(Path.Combine(root, "a")));
Console.WriteLine(Directory.Exists(Path.Combine(root, "a", "b")));
Console.WriteLine(new DirectoryInfo(deep).Name);
Console.WriteLine(Path.GetFileName(Path.GetDirectoryName(deep)));
Directory.Delete(root, true);
}
}
Output
True
True
c
b
How It Works
Four levels, one call. Neither a, b, nor c exists, yet a single CreateDirectory(deep) builds all three under root.
Parents were made too. The first two Exists checks look at the middle levels and both print True.
Last part versus its parent.DirectoryInfo.Name gives c. Path.GetDirectoryName removes that last part, and GetFileName then reads b.
One recursive delete.Delete(root, true) removes the whole demo tree. Without true it would fail because the folder has children.
Example 3 — Call It Twice and Hit a File Conflict
See that a repeat call is harmless, and what happens when a file already owns the name.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-create-twice");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
DirectoryInfo again = Directory.CreateDirectory(root);
Console.WriteLine(again.Exists);
string filePath = Path.Combine(root, "notes.txt");
File.WriteAllText(filePath, "hello");
try {
Directory.CreateDirectory(filePath);
} catch (IOException) {
Console.WriteLine("A file already uses that name");
}
Directory.Delete(root, true);
}
}
Output
True
A file already uses that name
How It Works
Second call is quiet. The folder is already there, so CreateDirectory changes nothing and returns a DirectoryInfo for it. There is no need to guard with Exists.
Make a file.File.WriteAllText writes notes.txt inside the folder, so that name now belongs to a file.
Wrong kind of thing. Asking for a folder with the same name as that file cannot work, so IOException is thrown and the catch block prints a message.
Tidy up. The recursive Delete removes the folder and the file in it.
Frequently Asked Questions
Yes. It creates every missing folder in the path, so Directory.CreateDirectory("a/b/c") builds a, then b, then c.
Nothing. The method does not throw and does not change the folder. It returns a DirectoryInfo for the existing folder, so you do not need to call Directory.Exists first.
A DirectoryInfo object. You can read its Name, FullName, Parent, and Exists properties, or call methods such as GetFiles on it.
It throws IOException when a file already has that name, UnauthorizedAccessException when you lack permission, ArgumentException for an empty or invalid path, and PathTooLongException for a very long path.
Use Path.Combine(Path.GetTempPath(), "my-folder") to build the path and pass it to Directory.CreateDirectory. It works on Windows, Linux, and macOS.