Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control recursive method depth - get all sub folders

I'm traveling through some shares to get information/permissions .. etc I'm using recursive to travel through all sub shares. it works fine however, the user should be able to limit the sub shares level to specific number which is a parameter in the application?

private static INodeCollection NodesLookUp(string path)
    {
        var shareCollectionNode = new ShareCollection(path);
        // Do somethings

       foreach (var directory in Directory.GetDirectories(shareCollectionNode.FullPath))
        {
            shareCollectionNode.AddNode(NodesLookUp(directory));

        }
        return shareCollectionNode;
    }

this code will go all way to the lowest level, how can i stop it in specific level? for example get all shares until 2 levels only?

Thanks.

like image 358
Maro Avatar asked Mar 09 '26 14:03

Maro


1 Answers

Rather than using a global variable to control the level, pass the maxLevel and decrement with each recursive call.

private static INodeCollection NodesLookUp(string path, int maxLevel)
{
    var shareCollectionNode = new ShareCollection(path);
    if (maxLevel > 0)
    {
        foreach (var directory in Directory.GetDirectories(shareCollectionNode.FullPath))
        {
            shareCollectionNode.AddNode(NodesLookup(directory, maxLevel-1));
        }
    }
    return shareCollectionNode;
}
like image 151
Jim Mischel Avatar answered Mar 11 '26 02:03

Jim Mischel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!