ios85
ios85

Reputation: 2134

Recursive search of file/folder structure

I am trying to build a recursive search function for a web service that returns a list of files and folders. I created the two methods so they act as recursive search, it first goes and gets the top level contents, then it adds any files to the fileList, and any sub folders to the subFoldersList. We pass in the access level (in our case root) and then the path which you want the information for. If any folders were found it then removes the top folder because it has begun the search for that folder. Then it calls the processDirectories method, which passes back to getFiles the new path location starting the process all over again. Right now for testing my folder structure is below. When it goes to add the second file (profilepic.png) to the list. I get an error "Collection was modified; enumeration operation may not execute." What is causing this error?

Photos
    picture1.png
    TestFolder
        profilepic.png

my code:

    public static List<string> fileList = new List<string>();
    public static List<string> subFolderList = new List<string>();

    static void processDirectories(string access, string Folder)
    {
        getFiles(access, Folder);
    }

    static void getFiles(string access, string Folder)
    {
        var accessToken = new OAuthToken(token, secret);
        var api = new DssAPI(ConsumerKey, ConsumerSecret, accessToken);
        var folder = api.GetContents(access, Folder);//Get list from WebService

        foreach (var item in folder.Contents)//Contents is an IEnumerable
        {
            if (item.IsDirectory == true)
                subFolderList.Add(item.Path);
            else
                fileList.Add(item.Path);
        }

        foreach (var subFolder in subFolderList)
        {
            subFolderList.RemoveAt(0);
            processDirectories(root, subFolder);
        }

    }

Upvotes: 3

Views: 2290

Answers (5)

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31193

You cannot go over the collection and modify it, as the error message says. For example the lower foreach is iterating the subFolderList and then you remove the first item. After that the iterators are not valid.

You should be using for loops, if you want to modify the collections, but then you have to remember to decrease the indexer variable if you delete the first item etc.

Upvotes: 0

Prasad Kanaparthi
Prasad Kanaparthi

Reputation: 6563

Try this,

Public static void GetFilesLocal( string path)
{
    foreach (string f in Directory.GetFiles( path))
    {
        // Add to subFolderList.
    }
    foreach (string d in Directory.GetDirectories( path))
    {
        GetFilesLocal( d ); 
    }
}

Upvotes: 1

YoryeNathan
YoryeNathan

Reputation: 14522

Change that:

foreach (var subFolder in subFolderList)
{
    subFolderList.RemoveAt(0);
    processDirectories(root, subFolder);
}

To:

while (subFolderList.Count > 0)
{
    var subFolder = subFolderList[0];
    subFolderList.RemoveAt(0);
    processDirectories(root, subFolder);
}

A collection cannot be modified while iterating through it, so when you're foreach-ing it and removing items from it inside the iteration, it causes trouble. The workaround is usually using a for loop and manipulating the loop-variable appropriately, but in your case a while loop is simpler.

Upvotes: 2

Austin Salonen
Austin Salonen

Reputation: 50235

Assuming you're not writing this as an academic exercise, you can use Directory.EnumerateFiles and avoid implementing this yourself.

foreach(var png in Directory.EnumerateFiles(sourceDirectory, "*.png", SearchOption.AllDirectories))
{
   // do something with the png file
}

Upvotes: 4

the problem is here

    foreach (var subFolder in subFolderList)
    {
        subFolderList.RemoveAt(0);
        processDirectories(root, subFolder);
    }

You're iterating over subFilderList, and you're removing items from it at the same time. The machine doesn't know how to handle that.

What I would suggest, in this case, is probably doing a regular for-loop

Upvotes: 1

Related Questions