Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Directories Structure

Tags:

c#

I am having a problem in creating multiple directories structures,

I know how to create a directory iam using these line of code to create a directory because there are more than 50 directories and 70 subdirectories in my project . I want to create directories structure at one one click

    private void button1_Click(object sender, EventArgs e)
    {
        string Path = "C:\\Test\\Test1";
        Directory.CreateDirectory(Path);
    }

But I want to creating directories structure like this

EXAMPLE :-

      1)string Path1 = "C:\\Test";

      2)string Path2 = "C:\\TestABC";

      3)string Path3 = "C:\\Test1\\123";

      4)string Path3 = "C:\\Test2\\145";

By this example i want to create this all strucure at a time.

There would be a great apreciation if someone could help me,

Thanks In Advance.

like image 335
G Basha Avatar asked Dec 10 '25 09:12

G Basha


2 Answers

lets assume that you have an array of strings containing all your directories you want to create.

string[] path = {...}; //all the directories
for(int i = 0; i < path.Length; i++)
{
    Directory.CreateDirectory(path[i]);
}

put this code in your function.

Edit: as you requested. your code would be something like this:

private void button1_Click(object sender, EventArgs e)
{
    string[] path = {"C:\\Test", "C:\\TestABC", "C:\\Test1\\123", "C:\\Test2\\145"}; //all the directories
    for(int i = 0; i < path.Length; i++)
    {
        Directory.CreateDirectory(path[i]);
    }
}
like image 51
Yasser Souri Avatar answered Dec 11 '25 22:12

Yasser Souri


Just to expand on Yasser's post in case your not sure how to fill in the path[] array.

vate void button1_Click(object sender, EventArgs e)
{
    string[] path = { 
                        "C:\\Test", 
                        "C:\\TestABC", 
                        "C:\\Test1\\123", 
                        "C:\\Test2\\145",
                        "C:\\AddMoreDirectoriesHere"
                    };
    for (int i = 0; i < path.Length; i++)
    {
        Directory.CreateDirectory(path[i]);
    }
}

Hope this helps

like image 33
Harag Avatar answered Dec 11 '25 22:12

Harag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!