C#创建文件夹结构的最佳方法

时间:2018-11-11 15:51:52

标签: c# directory directory-structure

我想自动生成不同的文件夹结构(作为新的Video / Film-Editing项目的准备)。它还允许向每个文件夹结构中添加特殊的文件夹,例如After Effects和Photoshop。

它应该从配置文件中读取结构,然后创建文件夹。

我当前的代码如下:

 if(tbPath.Text == "_______________________________________________________"
            || tbConfigPath.Text == "_______________________________________________________"
            || tbPath.Text == ""
            || tbConfigPath.Text == "")
        {
            System.Windows.MessageBox.Show("You didn't enter a valid Path.", "Invalid Path", MessageBoxButton.OK, MessageBoxImage.Error);
            return;
        }



        //List for the paths
        List<string> paths = new List<string>();
        List<string> finalpaths = new List<string>();
        string outPath = tbPath.Text;

        //Where to get the config from
        string configPath = tbConfigPath.Text;


        string line = "";
        // Read the file and display it line by line.  
        System.IO.StreamReader file = new System.IO.StreamReader(configPath);
        while ((line = file.ReadLine()) != null)
        {
            paths.Add(line);
        }

        file.Close();

        for (int i = 0; i < paths.Count(); i++)
        {
            finalpaths.Add(outPath + paths[i]);
        }

        //----------------Folder Generatring------------
        for (int i = 0; i < paths.Count(); i++)
        {
            Directory.CreateDirectory(finalpaths[i]);
        }


        // Add After Effects
        if (cbAE.IsChecked == true)
            {
            string AEpath = outPath + "\\AfterEffects";
            Directory.CreateDirectory(AEpath);
        }

        // Add Photoshop
        if (cbAE.IsChecked == true)
        {
            string PSpath = outPath + "\\Photoshop";
            Directory.CreateDirectory(PSpath);
        }


        System.Windows.MessageBox.Show("The folders where generated successfully", "Success", MessageBoxButton.OK, MessageBoxImage.Information);

        pgStartUp pgStart = new pgStartUp();
        NavigationService.Navigate(pgStart);

但是我觉得这不是很有效。有什么更好的方法吗?

1 个答案:

答案 0 :(得分:4)

使用时:

Directory.CreateDirectory(path);

此行在您作为参数传递的路径字符串中创建所有或缺少的目录。因此,您不需要其他任何东西。

考虑到根目录和类似内容,您所有的逻辑都可以创建正确的路径字符串