Why the question "create a folder if it doesn't exist" became a meme in C#
If you've ever written code that saves files, processes reports, or caches data, you've likely encountered the situation: "what if the folder doesn't exist?". It seems simple enough, right? Check — create. But in practice, this task turns into a real quest with bugs, slowdowns, and exceptions.
On Stack Overflow, the question "If a folder does not exist, create it" has garnered hundreds of thousands of views. And this is no accident. Even experienced developers sometimes write code that seems to work but kills performance or crashes in multithreaded scenarios.
In this article, we'll break down how to properly create a folder in C# if it doesn't exist, without stepping on rakes. You'll learn why Directory.CreateDirectory is not just "create" but a powerful optimization tool, and how to write code that doesn't slow down even on slow disks.
The most common (and incorrect) approach
When a beginner first encounters this task, they usually write something like:
if (!Directory.Exists(path)){ Directory.CreateDirectory(path);}At first glance — it's logical. Checked that the folder doesn't exist, created it. But let's look at what happens under the hood.
Problem #1: Double file system call
Directory.Exists makes a request to the file system. Directory.CreateDirectory makes another request (or several). In the end, we access the disk twice. On an SSD this isn't critical, but on network folders or slow HDDs, every extra operation means lost milliseconds. And what if there are hundreds of such checks?
Problem #2: Race condition
Imagine two threads executing this code simultaneously. Both check — the folder doesn't exist. Both try to create it. One creates successfully, the second crashes with an IOException ("Folder already exists"). Or, even worse, both create it, but one overwrites the access rights set by the other.
Problem #3: False sense of security
Many think: "I checked, so everything is fine." But between the check and the creation, a microsecond can pass during which another process creates that folder. Or the antivirus blocks the creation. Or the disk runs out of space.
The correct approach: Directory.CreateDirectory is your friend
And now for the surprise: Microsoft's documentation explicitly states that Directory.CreateDirectory itself checks whether the folder exists. If it exists — it does nothing (returns a DirectoryInfo object for the existing folder). If not — it creates it.
That means the entire if block is redundant! It's enough to just write:
Directory.CreateDirectory(path);This is not only shorter, but also more performant and safer in a multithreaded environment. The method is atomic from the file system's perspective: it either creates the folder, or confirms it already exists, and returns control.
Why is it faster?
Because internally, CreateDirectory uses the WinAPI function CreateDirectory, which, if the folder exists, simply returns ERROR_ALREADY_EXISTS. No extra directory traversal. One system call instead of two.
Deep dive: how Directory.CreateDirectory works
Let's look at the actual code. The method not only creates the final folder but also all parent directories in the path. This is another reason not to write your own logic.
string path = @"C:\\Projects\\Reports\\2025\\January\\Data";Directory.CreateDirectory(path);// If C:\\Projects doesn't exist — it will create it.// If Reports doesn't exist — it will create it.// And so on, up to Data.Without this, you would have to recursively split the path and create