开发者

How to create Directories Structure

开发者 https://www.devze.com 2023-03-12 00:22 出处:网络
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 7

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";

 开发者_如何学C     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 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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号