C# – How to create directories

You can use Directory.CreateDirectory() to create a directory at the specified path (absolute or relative), like this:

using System.IO; Directory.CreateDirectory(@"C:\coding\dotnet\examples\");
Code language: C# (cs)

This creates the directory if it doesn’t exist, otherwise it does nothing. That means you don’t need to check if the directory exists before calling Directory.CreateDirectory().

In this article, I’ll go into more details about using Directory.CreateDirectory().

CreateDirectory() creates all missing directories in the path

When you use Directory.CreateDirectory() to create a directory, it creates all missing directories in the specified path. This is very convenient.

Here’s an example to show this behavior. Let’s say you want to create directory C:\Test\HelloWorld\ and neither the \Test\ directory nor the \Test\HelloWorld\ directory exist. Directory.CreateDirectory() creates both the missing directories.

using System.IO; Console.WriteLine("Before creating"); Console.WriteLine($@"C:\Test\ exists? {Directory.Exists(@"C:\Test\")}"); Console.WriteLine($@"C:\Test\HelloWorld\ exists? {Directory.Exists(@"C:\Test\HelloWorld\")}"); Directory.CreateDirectory(@"C:\Test\HelloWorld\"); Console.WriteLine("After creating"); Console.WriteLine($@"C:\Test\ exists? {Directory.Exists(@"C:\Test\")}"); Console.WriteLine($@"C:\Test\HelloWorld\ exists? {Directory.Exists(@"C:\Test\HelloWorld\")}");
Code language: C# (cs)

This outputs the following, showing the neither directory existed and then Directory.CreateDirectory() created both of them:

Before creating C:\Test\ exists? False C:\Test\HelloWorld\ exists? False After creating C:\Test\ exists? True C:\Test\HelloWorld\ exists? True
Code language: plaintext (plaintext)

Check if a directory exists

Directory.CreateDirectory() creates a directory if it doesn’t exist, otherwise it does nothing. You can use Directory.Exists() if you want to check if the directory exists. This may be useful for things like logging when you had to create the directory. Here’s an example:

using System.IO; var dirPath = @"C:\HelloWorld\"; if (!Directory.Exists(dirPath)) { Console.WriteLine("Directory doesn't exist. Will create it"); Directory.CreateDirectory(dirPath); }
Code language: C# (cs)

This outputs the following and creates the directory:

Directory doesn't exist. Will create it
Code language: plaintext (plaintext)

Create a directory with a relative path

You can specify an absolute or relative path when creating a directory. When you use a relative path, it’s relative to the current working directory – which can be found in Environment.CurrentDirectory.

Here’s an example of creating a directory using a relative path and then displaying the full path:

using System.IO; var directoryInfo = Directory.CreateDirectory("data"); Console.WriteLine($"Created directory path: {directoryInfo.FullName}"); Console.WriteLine($"Current working directory: {Environment.CurrentDirectory}");
Code language: C# (cs)

This outputs the following:

Created directory path: D:\Projects\HelloWorld\data Current working directory: D:\Projects\HelloWorld
Code language: plaintext (plaintext)

Use the created directory’s DirectoryInfo

Directory.CreateDirectory() returns a DirectoryInfo object for the directory it created (or for the existing directory). You can use this to check information about the directory and perform tasks (such as creating a subdirectory).

Here’s an example of using the returned DirectoryInfo to create a few subdirectories:

using System.IO; var dirInfo= Directory.CreateDirectory(@"C:\Projects\HelloWorld\"); dirInfo.CreateSubdirectory("src"); dirInfo.CreateSubdirectory("tests"); dirInfo.CreateSubdirectory("data");
Code language: C# (cs)

Notice that you can use relative paths with Directory.CreateSubdirectory() (the paths are relative to the created directory’s path), which is a convenient way to create subdirectories in the newly created directory.

Leave a Comment