Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8802445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:05:35+00:00 2026-06-14T01:05:35+00:00

I am trying to build a recursive search function for a web service that

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:05:36+00:00Added an answer on June 14, 2026 at 1:05 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a class that inherits methods from Python's list, but also
I'm trying to write a recursive function that return a List < int *
Problem: I am trying to build a recursive tree using a function and data
I am currently trying to build a program where I have a recursive function
I am trying to build a list from a sequence by recursively appending the
I'm trying build a method which returns the shortest path from one node to
I am trying to write a function that converts between Maps with integer keys
I'm trying to build an unordered list from an existing XML file which contains
I'm trying to write C# Chess AI. At that moment I have to build
I'm basically trying to build an html ul/li nested list from a multidimensional array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.