In this C# tutorial, I will explain how to create a folder with subfolders in C#.
To create folders with subfolders in C# first, use the Directory.CreateDirectory method and then specify the path of the main folder. Then, append the subfolder paths to the main folder path and use CreateDirectory for each. This method ensures the creation of both the main folder and any nested subfolders in your specified directory.
The System.IO
namespace in C# provides classes that allow you to read and write, both synchronously and asynchronously, on data streams and files. The classes we’re going to focus on are Directory
and DirectoryInfo
.
Class | Description |
---|---|
Directory | Provides static methods for directory operations. |
DirectoryInfo | Offers instance methods for directory operations. |
Create a Single Folder in C#
Creating a single folder is straightforward in C#. The Directory.CreateDirectory
method can be used to create a single folder in C#. Here’s a small snippet to demonstrate:
using System;
using System.IO;
namespace CreateFolder
{
class Program
{
static void Main(string[] args)
{
string folderPath = @"C:\ExampleFolder";
Directory.CreateDirectory(folderPath);
Console.WriteLine("Folder created!");
}
}
}
Create a Folder with Subfolders in C#
Now, we will see how to create a folder with subfolders in C#.
Using Directory.CreateDirectory
Here’s an example using Directory.CreateDirectory
, to create multiple subfolders in C#:
static void CreateSubfolders()
{
string path = @"C:\MainFolder\Sub1\Sub2";
Directory.CreateDirectory(path);
Console.WriteLine("Subfolders created!");
}
Using DirectoryInfo
If you need more control over folder attributes or other properties, you might opt for the DirectoryInfo
class.
static void CreateSubfoldersWithInfo()
{
DirectoryInfo dirInfo = new DirectoryInfo(@"C:\AnotherMainFolder");
dirInfo.Create();
DirectoryInfo subDirInfo = dirInfo.CreateSubdirectory(@"Sub1\Sub2");
Console.WriteLine("Subfolders created with DirectoryInfo!");
}
Here is a complete example of creating a folder with subfolders in C# using different methods.
using System;
using System.IO;
namespace DirectoryManagement
{
class Program
{
static void Main(string[] args)
{
// Create a single folder
CreateFolder();
// Create multiple subfolders
CreateSubfolders();
// Create subfolders using DirectoryInfo
CreateSubfoldersWithInfo();
}
static void CreateFolder()
{
string path = @"C:\NewFolder";
Directory.CreateDirectory(path);
Console.WriteLine("Folder created!");
}
static void CreateSubfolders()
{
string path = @"C:\MainFolder\Sub1\Sub2";
Directory.CreateDirectory(path);
Console.WriteLine("Subfolders created!");
}
static void CreateSubfoldersWithInfo()
{
DirectoryInfo dirInfo = new DirectoryInfo(@"C:\AnotherMainFolder");
dirInfo.Create();
DirectoryInfo subDirInfo = dirInfo.CreateSubdirectory(@"Sub1\Sub2");
Console.WriteLine("Subfolders created with DirectoryInfo!");
}
}
}
Once you run the code using the Visual Studio console application, you can see the output below:
This is how to create a folder with subfolders in C#.
Conclusion
In this C# tutorial, I have explained a complete example with code on how to create a folder with subfolders in C#.
You may also like:
- How to Create a Folder with a Date in C#?
- Delete Oldest Files in a Directory Using C#
- Create a Folder in the Current Directory Using C#
- C# Folder Naming Conventions
- How to Delete a Folder with Subfolders and Files in C#?
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…