gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:55:22 25 4
gpt4 key购买 nike

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

但我觉得这样效率不高。有没有更好的办法?

最佳答案

当您使用时:

Directory.CreateDirectory(path);

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

您的所有逻辑都应专注于创建正确的路径字符串,同时考虑根目录和类似的东西。

关于C# 创建文件夹结构的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53250448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com