How to Create a Folder with Subfolders in C#?

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.

ClassDescription
DirectoryProvides static methods for directory operations.
DirectoryInfoOffers 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:

Create a Folder with Subfolders in C#

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: