Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Multiple Sub Folders inside a Folder

Tags:

c#

I want to make multiple sub folders in a folder at once. Multiple Folders

Like in image i want to create A->B->C and D inside B all at once without loop. Is there any way to achieve it in C#

like image 775
Pankaj Bhatia Avatar asked Jul 26 '26 03:07

Pankaj Bhatia


1 Answers

Directory.CreateDirectory will create all directories in the given path, including any subdirectories.

using System.IO;

var paths = new [] { "F:\\A\\B\\C", "F:\\A\\B\\D" };

foreach (var path in paths) {
    try {
        // Determine whether the directory exists.
        if (Directory.Exists(path)) {
            Console.WriteLine($"Skipping path '{path}' because it exists already.");
            continue;
        }

        // Try to create the directory.
        var di = Directory.CreateDirectory(path);
        Console.WriteLine($"Created path '{path}' successfully at {Directory.GetCreationTime(path)}.");
    }
    catch (Exception e) {
        Console.WriteLine($"The process failed: {e}");
    }
}
like image 61
Georg Patscheider Avatar answered Jul 27 '26 18:07

Georg Patscheider