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 8976441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:08:08+00:00 2026-06-15T19:08:08+00:00

I am using C# 4.5 and I’m trying to scan a fileshare for all

  • 0

I am using C# 4.5 and I’m trying to scan a fileshare for all folders. I want to skip any folder I cannot access and just continue. What I have done is to do it recursively, this throws a stackoverflow. I understand why this happens. So my question is: are there any work arounds?

How would you implemented this since we cannot use recursive search? Are there any third-party libraries I can use to simplify this? GetFolder function is only extracting some information and return a custom class, this works fine.

public void GetFoldersFromFS(string filePath)
{
   if (filePath == null)
   {
      return;
   }

   Directory.SetCurrentDirectory(filePath);
   try
   {
       foreach (var directory in Directory.EnumerateDirectories(Directory.GetCurrentDirectory()))
       {
           Resources.Add(GetFolder(new DirectoryInfo(directory)));
           GetFoldersFromFS(directory);
       }
   }
   catch (UnauthorizedAccessException e)
   {
      Log.Warn(e.Message);
   }
   catch (PathTooLongException e)
   {
      Log.Warn(e.Message);
   }
}
  • 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-15T19:08:10+00:00Added an answer on June 15, 2026 at 7:08 pm
    1. Make a list for the directories that you need to do (the to-do-list).
    2. Initially, add the single directory (on the share) to the to-do-list.
    3. Take the first directory from the list and scan it.
    4. When you scan a directory, add any subdirectories to the to-do-list (at the end of the list).
    5. Go back to 3 until the to-do-list is empty.

    Voila, scanning without recursion.

    Pseudo code (without any try-catch):

    public List<string> ScanDirectory(string directory) {
        var toDoList = new Queue<string>();
        var result = new List<string>();
        toDoList.Enqueue(directory);
    
        // Keep going while there is anything to do
        while (toDoList.Count > 0) {
            // Get next directory-to-scan, and add it to the result
            var currentDir = toDoList.Dequeue();
            result.Add(currentDir);
            // Get sub directories
            var subDirectories = new DirectoryInfo(currentDir).GetDirectories();    // TODO: Add any other criteria you want to check
            // Add the sub directories to the to-do list
            foreach (var subDirectory in subDirectories) {
                toDoList.Enqueue(subDirectory);
            }
        }
    
        // Return all found directories
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using C#, I want to show the image in the Access column but failed
Using a populated Table Type as the source for a TSQL-Merge. I want to
Using the Redis info command, I am able to get all the stats of
Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.
Using Microsoft SQL Server 2005, is there any way to see when a table
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using linq2sql I'm trying to take the string in txtOilChange and update the oilChange
Using the navigator.geolocation object in JavaScript. Trying to establish accurate ranges, but wondering exactly
Using Rails 3.2.0 with haml, sass and coffeescript: Basically I am trying to disable

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.