Recursive search of file/folder structure

做~自己de王妃 提交于 2019-12-23 23:13:03

问题


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);
        }

    }

回答1:


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.




回答2:


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
}



回答3:


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




回答4:


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 ); 
    }
}



回答5:


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.



来源:https://stackoverflow.com/questions/13294390/recursive-search-of-file-folder-structure

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