Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a Directory using C#?
To create, move, and delete directories in C#, the System.IO.Directory class provides essential methods for directory operations. The Directory.CreateDirectory() method is used to create new directories at specified paths.
Syntax
Following is the syntax for creating a directory using Directory.CreateDirectory() method −
Directory.CreateDirectory(string path);
Following is the syntax for checking if a directory exists before creating it −
if (!Directory.Exists(path)) {
Directory.CreateDirectory(path);
}
Parameters
-
path − A string representing the directory path to create. Can be an absolute or relative path.
Return Value
The Directory.CreateDirectory() method returns a DirectoryInfo object representing the created directory.
Creating a Simple Directory
The following example demonstrates how to create a directory after checking if it already exists −
using System;
using System.IO;
class Program {
public static void Main() {
string myDir = @"D:\NEW";
if (!Directory.Exists(myDir)) {
Directory.CreateDirectory(myDir);
Console.WriteLine("Directory created successfully: " + myDir);
} else {
Console.WriteLine("Directory already exists: " + myDir);
}
}
}
The output of the above code is −
Directory created successfully: D:\NEW
Creating Nested Directories
The Directory.CreateDirectory() method can create multiple nested directories in a single call, even if the parent directories don't exist −
using System;
using System.IO;
class Program {
public static void Main() {
string nestedDir = @"C:\MyApp\Data\Logs\Archive";
Directory.CreateDirectory(nestedDir);
Console.WriteLine("Nested directories created: " + nestedDir);
// Verify creation
if (Directory.Exists(nestedDir)) {
Console.WriteLine("Directory verification: SUCCESS");
}
}
}
The output of the above code is −
Nested directories created: C:\MyApp\Data\Logs\Archive Directory verification: SUCCESS
Creating Directory with Exception Handling
It's good practice to handle potential exceptions when creating directories −
using System;
using System.IO;
class Program {
public static void Main() {
string dirPath = @"C:\TestFolder\SubFolder";
try {
DirectoryInfo dirInfo = Directory.CreateDirectory(dirPath);
Console.WriteLine("Directory created at: " + dirInfo.FullName);
Console.WriteLine("Creation time: " + dirInfo.CreationTime);
}
catch (UnauthorizedAccessException ex) {
Console.WriteLine("Access denied: " + ex.Message);
}
catch (DirectoryNotFoundException ex) {
Console.WriteLine("Path not found: " + ex.Message);
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
The output of the above code is −
Directory created at: C:\TestFolder\SubFolder Creation time: 12/15/2023 10:30:45 AM
Key Features
-
Directory.CreateDirectory()creates the entire directory path if it doesn't exist. -
If the directory already exists, the method returns the existing directory without throwing an exception.
-
Parent directories are automatically created if they don't exist.
-
The method works with both absolute and relative paths.
Conclusion
The Directory.CreateDirectory() method in C# provides a simple way to create directories and nested directory structures. It automatically handles the creation of parent directories and safely handles cases where directories already exist, making it ideal for robust file system operations.
