【问题标题】:Search through directory and creating folders搜索目录并创建文件夹
【发布时间】:2018-08-31 15:33:21
【问题描述】:

我想在一个目录中的子文件夹中创建多个文件,但似乎无法考虑清楚。在下图中,我想要它做的是在主目录中查找(在本例中为“文件位置”),然后转到子文件夹,然后进入其中的下一个子文件夹,如果该文件夹不存在将创建它。

所以路径是:"File Location\2015\15-01",它将查找名为"Hello" 的子文件夹,如果它不存在,它将创建该文件夹,然后遍历"File Location" 中的所有子文件夹。

如何通过 c# 创建它?

【问题讨论】:

  • 请为您的问题选择一种语言标签(C# 或批处理)。
  • 批处理文件可以使用for 循环遍历目录中的所有条目。您可以使用if exist filename 检查是否存在这也可以递归。问题是,你想在这些子文件夹中做什么?

标签: c#


【解决方案1】:

试试这个。下面的代码是针对c#的

string path = @"......."; //Your complete path
string[] directories = path.Split(Path.DirectorySeparatorChar);

它将为您提供文件夹名称。然后你可以检查每条路径

var i = 0;
string folderPath = directories[i];

while(i < directories.length)
{
        bool exists = System.IO.Directory.Exists(folderPath);

        if(!exists)
            System.IO.Directory.CreateDirectory(folderPath);

        i++;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多