【问题标题】:C# Best way to create folder-structuresC# 创建文件夹结构的最佳方法
【发布时间】:2019-04-14 11:34:53
【问题描述】:

我想自动生成不同的文件夹结构(作为新视频/电影编辑项目的准备)。它还允许向每个文件夹结构添加特殊文件夹,例如 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);

但我觉得这不是很有效。有没有更好的办法?

【问题讨论】:

    标签: c# directory directory-structure


    【解决方案1】:

    作为编码人员,我们经常在代码中思考并尝试用它解决所有问题。 如果基本结构相同,则创建一次文件夹结构,将其压缩,然后:

    Directory.CreateDirectory(tbPath.Text);
    ZipFile.ExtractToDirectory("yadayada.zip", tbPath.Text);
    
            // 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);
            }
    

    【讨论】:

      【解决方案2】:

      使用时:

      Directory.CreateDirectory(path);
      

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

      您的所有逻辑都可以创建一个正确的路径字符串,同时考虑到根目录等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-01
        • 1970-01-01
        • 2016-09-29
        • 1970-01-01
        • 2012-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多