简体   繁体   English

如何创建目录结构

[英]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.我知道如何使用这些代码行创建目录来创建目录,因为我的项目中有 50 多个目录和 70 个子目录。 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.将此代码放入您的 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只是扩展 Yasser 的帖子,最好在创建目录之前检查目录是否存在

 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.只是为了扩展 Yasser 的帖子,以防您不确定如何填写 path[] 数组。

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希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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