简体   繁体   中英

How to create Directories Structure

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.

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]);
    }
}

Just expending Yasser's post its better to check if directory exists before creating a directory

 private void button1_Click(object sender, EventArgs e)
    {
        //all the directories
        string[] path = {"C:\\Test", "C:\\TestABC", "C:\\Test1\\123", "C:\\Test2\\145"}; 
        for(int i = 0; i < path.Length; i++)
        {
           if(!Directory.Exists(path[i])
                 Directory.CreateDirectory(path[i]);
        }
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM