How to Limit The Depth of a Recursive Sub-Directory Search

安稳与你 提交于 2019-12-18 16:50:41

问题


I've got a function that currently grabs all folders and sub-folders to check the ACL's for a small tool I'm building but I'm pulling my hair out trying to figure out how to limit the depth that it can go to. For example you have a folder that goes 4 levels deep but I want to be able to only grab 3 levels of it for ACL's.

Currently I have it coded thusly:

private void StepThroughDirectories(string dir)
{
    string[] directories = Directory.GetDirectories(dir);
    try
    {
        foreach (string d in Directory.GetDirectories(dir))
        {
            if (recCount < (int)Depth)
            {
                GetACLs(d, new DirectoryInfo(d));
                pBar.Value += 1;
                //MessageBox.Show("Recursive Level: " + counter.ToString());
                recCount++;
                StepThroughDirectories(d);
            }
            else
            {
                recCount--;
            }
        }
    }
    catch (System.Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

Obviously that's not as nice as it was because I've been working on the problem for a little while but if anyone can point me in the right direction to solve this issue I would be very happy!


回答1:


First, avoid declaring the recCount field outside as a “global” variable. In recursive scenarios it's usually more manageable to pass state along the recursive calls.

Second, move the depth test out of the foreach to remove unnecessary querying of the file system for subdirectories.

Third, place the actual processing logic at the beginning of your method, again out of the subdirectories processing loop.

Your code would then look like:

void StepThroughDirectories(string dir)
{
    StepThroughDirectories(dir, 0)
}

void StepThroughDirectories(string dir, int currentDepth)
{
    // process 'dir'
    ...

    // process subdirectories
    if (currentDepth < MaximumDepth)
    {
        foreach (string subdir in Directory.GetDirectories(dir))
            StepThroughDirectories(subdir, currentDepth + 1);
    }
}



回答2:


One possible method, add a class field outside your method and a variable to indicate how many levels deep to go max.

int levels;

private void StepThroughDirectories(string dir, int depth)
{
    levels ++;
    if (levels > depth)
        return;
    string[] directories = Directory.GetDirectories(dir);
    try
    { ...



回答3:


Decrement recCount when you return from StepThroughDirectories, but this would be better...

    private void StepThroughDirectories(string dir, int depth)
    {
        if (depth < 0)
            return;
        string[] directories = Directory.GetDirectories(dir);
        try
        {
            foreach (string d in Directory.GetDirectories(dir))
            {
                // your code here
                Console.WriteLine("{0}", d);
                StepThroughDirectories(d, depth-1);
            }
        }
        catch (System.Exception e)
        {
            Console.WriteLine(e.Message);
        }
    } 


来源:https://stackoverflow.com/questions/3874516/how-to-limit-the-depth-of-a-recursive-sub-directory-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!