【问题标题】:Get a list of the sub folders under a directory获取目录下的子文件夹列表
【发布时间】:2016-05-24 19:11:40
【问题描述】:

好的,这是我目前的问题:

这是我的主要内容:

//System prompts user the type of archiving which will direct to the correct directory to set as root
Console.WriteLine("Enter the type of archiving you would like to do?");
string archivetype = Console.ReadLine(); 

//function that identifies which candidates to archive
Archive(archivetype, 20, 20);

//keep the application in debug mode
Console.ReadKey();

存档方式:

//Archive method determines which directory to search in and how many versions to archive
static void Archive(string archivetype, int pversion, int version)
{
    //regex pattern to get folder names of the type #.#.#.#/#. something
    Regex reg = new Regex(@"\d+(\.\d+)+");
    //setting where to start looking
    DirectoryInfo root = new           DirectoryInfo(@"C:\Users\jphillips\Desktop\test\parent\ACE-3_0");
    var dirs = new List<DirectoryInfo>();
    //i want to make a recursive call to all the folders in my root directory to obtain all the folders with the regex pattern above that are not empty and do not have 3 files inside
    WalkDirectoryTree(root);      
}

终于走目录树方法

//so im using the walk directory tree on the microsoft website i need a way to have a sort of a global array to keep adding the directories that fit through the patterns mentioned above without resetting itself after each walkdirectorytree call
static void WalkDirectoryTree(System.IO.DirectoryInfo root)
{
    DirectoryInfo[] subDirs = null;

    // Now find all the subdirectories under this root directory.
    subDirs = root.GetDirectories();

    foreach (DirectoryInfo dir in subDirs)
    {
        //dirs is not global so it doesnt work here and i believe if i put a local array that it will reset itself everytime
        dirs = root.GetDirectories("*", SearchOption.TopDirectoryOnly).Where(d => reg.IsMatch(d.Name)).ToList();
        if()
        WalkDirectoryTree(dir);
    }
}

所以我在这一点上真的迷路了,我希望能够调用 walkdirectorytree 以递归方式遍历我目录的所有文件夹和子文件夹,以提取具有正则表达式模式且不为空且内部没有 3 个文件的路径tp 给我这些文件夹路径的列表。

【问题讨论】:

    标签: c# directory getdirectories


    【解决方案1】:

    您可以通过 GetDirectories 的重载在一次调用中获取所有文件夹和子文件夹。

    您传入了一个搜索字符串 - 但不幸的是,不是正则表达式,SearchOption.AllDirectories 作为第二个参数。然后,您可以通过正则表达式传递结果以找到您感兴趣的结果。

    【讨论】:

    • 好的,这就是我要做的:dirs = root.GetDirectories("*", SearchOption.AllDirectories).Where(d => reg.IsMatch(d.Name)).ToList() .Select(w => w.);
    • 在选择我将如何删除例如,如果我的文件夹末尾有 TESTS。所以正常格式是 4.3.2.4 但有些文件夹有 4.45.3.3_TESTS
    • @PhillipsJP 为这种情况获得一个有效的正则表达式可能值得一个单独的问题。
    猜你喜欢
    • 2010-12-26
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    相关资源
    最近更新 更多