How to create a folder if it does not exist in C#?

Creating directories programmatically is a common task in C# applications. The System.IO namespace provides the necessary classes and methods to check for directory existence and create new folders when needed.

It is always recommended to check if a directory exists before performing any file operations in C#, as the compiler will throw an exception if you attempt to access a non-existent folder.

Syntax

Following is the syntax for checking if a directory exists −

bool exists = Directory.Exists(path);

Following is the syntax for creating a directory −

Directory.CreateDirectory(path);

Using Directory.Exists() and Directory.CreateDirectory()

The Directory.Exists() returns true if the specified directory exists, and false otherwise. The Directory.CreateDirectory() method creates all directories and subdirectories in the specified path unless they already exist.

Directory Creation Process Check Exists Directory.Exists() Exists? No Create Directory Directory.CreateDirectory() Yes Done

Example

using System;
using System.IO;

namespace DemoApplication {
   class Program {
      static void Main(string[] args) {
         string folderName = @"C:\TempDemo\MyFolder";
         
         // Check if directory exists before creating
         if (!Directory.Exists(folderName)) {
            Directory.CreateDirectory(folderName);
            Console.WriteLine("Directory created: " + folderName);
         } else {
            Console.WriteLine("Directory already exists: " + folderName);
         }
         
         // Verify the directory was created
         Console.WriteLine("Directory exists: " + Directory.Exists(folderName));
      }
   }
}

The output of the above code is −

Directory created: C:\TempDemo\MyFolder
Directory exists: True

Creating Nested Directories

The Directory.CreateDirectory() method automatically creates all intermediate directories in the path if they don't exist −

Example

using System;
using System.IO;

namespace DemoApplication {
   class Program {
      static void Main(string[] args) {
         string nestedPath = @"C:\Projects\WebApp\Controllers\API\v1";
         
         if (!Directory.Exists(nestedPath)) {
            Directory.CreateDirectory(nestedPath);
            Console.WriteLine("Nested directories created successfully");
         }
         
         // List all directories in the path to verify creation
         string[] directories = Directory.GetDirectories(@"C:\Projects", "*", SearchOption.AllDirectories);
         Console.WriteLine("Created directories:");
         foreach (string dir in directories) {
            Console.WriteLine("- " + dir);
         }
      }
   }
}

The output of the above code is −

Nested directories created successfully
Created directories:
- C:\Projects\WebApp
- C:\Projects\WebApp\Controllers
- C:\Projects\WebApp\Controllers\API
- C:\Projects\WebApp\Controllers\API\v1

Using DirectoryInfo Class

An alternative approach is to use the DirectoryInfo class, which provides an object-oriented way to work with directories −

Example

using System;
using System.IO;

namespace DemoApplication {
   class Program {
      static void Main(string[] args) {
         string folderPath = @"C:\MyApp\Logs";
         DirectoryInfo dir = new DirectoryInfo(folderPath);
         
         if (!dir.Exists) {
            dir.Create();
            Console.WriteLine("Directory created using DirectoryInfo");
            Console.WriteLine("Full path: " + dir.FullName);
            Console.WriteLine("Creation time: " + dir.CreationTime);
         } else {
            Console.WriteLine("Directory already exists");
         }
      }
   }
}

The output of the above code is −

Directory created using DirectoryInfo
Full path: C:\MyApp\Logs
Creation time: 12/15/2024 10:30:45 AM

Comparison of Methods

Method Type Best Use Case
Directory.CreateDirectory() Static method Simple directory creation tasks
DirectoryInfo.Create() Instance method When you need additional directory information
Directory.Exists() Static method Quick existence check before operations

Conclusion

Creating directories in C# is straightforward using the System.IO namespace. Always check for directory existence using Directory.Exists() before creating folders with Directory.CreateDirectory(). The method automatically handles nested directory creation, making it safe and convenient for most scenarios.

Updated on: 2026-03-17T07:04:36+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements