Peter Byte
Peter Byte

Reputation: 1

C# Recursive folder search does not work properly

I need help, because I've written a method that should find a special directory on a computer that definitely exists. First I've written a method that will be go through every drive on a computer and open up for every drive the recursive method.

private string LocateOnDrives()
    {
        string result = string.Empty;
        string[] drives = Directory.GetLogicalDrives();

        foreach (string drive in drives)
        {
            string buffer = this.Locate(drive);

            if (buffer.EndsWith(SEARCH_PATTERN))
            {
                return buffer;
            }
        }

        return result;
    }

And this is the method that is called for every drive:

private string Locate(string directory)
    {
        string result = string.Empty;

        try
        {
            string[] dirs = Directory.GetDirectories(directory);

            foreach (string dir in dirs)
            {
                if (dir.EndsWith(SEARCH_PATTERN))
                {
                    return dir;
                }
                else
                {
                    result = this.Locate(dir);
                }
            }
        }
        catch { }            

        return result;
    }

The try catch is necessary, because there are some directories with no permission. I have the permission for the sought folder and when i debug this, it will jump into the if condition that it has found and set the local 'result' to this sought folder. Up to this point it really makes that what was my intention. But the recursive method will search further and the overall return is string.Empty!

I already did something link this:

private string tragetDir;
    private string Locate(string directory)
    {
        string result = string.Empty;

        try
        {
            string[] dirs = Directory.GetDirectories(directory);

            foreach (string dir in dirs)
            {
                if (dir.EndsWith(DEFAULT_GTAV_DIRECTORY_NAME))
                {
                    targetDir = dir;
                }
                else
                {
                    result = this.Locate(dir);
                }
            }
        }
        catch { }            

        return result;

This is working for me, but not what I wanted to have, because it should be possible that the recursive method will return this wanted folder…

It is late for me and I just want to fix this little mistake! Can someone help me out, because I am desperate, THANKS!

Upvotes: 0

Views: 70

Answers (2)

Peter Byte
Peter Byte

Reputation: 1

This is a solution I tried and it worked for me now:

private string Locate(string directory)
    {
        string result = string.Empty;

        string[] dirs = new string[0];

        try
        {
            dirs = Directory.GetDirectories(directory);
        }
        catch { /* Ignore */ }            

        foreach (string dir in dirs)
        {
            if (dir.EndsWith(SEARCH_PATTERN))
            {
                result = dir;
                break;
            }
            else
            {
                result = this.Locate(dir);

                if (result.EndsWith(SEARCH_PATTERN))
                {
                    break;
                }
            }                
        }           
        return result;
    }

First I had to check if the current "dir" in the loop was already the sought folder. If not, the loop had to browse inside this folder and if the result inside this folder isn't the sought folder the loop had to going on and search on or in the next folder in loop. In any case that the right directory was found, the loop will "break" and return the result! This is it!

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66449

When you find a match and return it, then unwind once in your nested calls to Locate(), you assign the match to result but then keep progressing with the loop, when you actually want to break out of it.

result = this.Locate(dir, SEARCH_PATTERN);
if (result.EndsWith(SEARCH_PATTERN))
{
    break;
}

Also, you might consider just catching the UnauthorizedAccessException since that's the one it'll throw if you don't have permission to a particular directory.

Upvotes: 1

Related Questions