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

  • Home
  • SEARCH
  • 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 8768381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:03:07+00:00 2026-06-13T17:03:07+00:00

I have recently had a need to Enumerate an entire file system looking for

  • 0

I have recently had a need to Enumerate an entire file system looking for specific types of files for auditing purposes. This has caused me to run into several exceptions due to having limited permissions on the file system to be scanned. Among them, the most prevalent have been UnauthorizedAccessException and much to my chagrin, PathTooLongException.

These would not normally be an issue except that they invalidate the IEnumerable, preventing me from being able to complete the scan.

  • 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-13T17:03:07+00:00Added an answer on June 13, 2026 at 5:03 pm

    In order to solve this problem, I have created a replacement File System Enumerator. Although it may not be perfect, it performs fairly quickly and traps the two exceptions that I have run into. It will find any directories or files that match the search pattern passed to it.

    // This code is public domain
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using log4net;
    
    public class FileSystemEnumerable : IEnumerable<FileSystemInfo>
    {
        private ILog _logger = LogManager.GetLogger(typeof(FileSystemEnumerable));
    
        private readonly DirectoryInfo _root;
        private readonly IList<string> _patterns;
        private readonly SearchOption _option;
    
        public FileSystemEnumerable(DirectoryInfo root, string pattern, SearchOption option)
        {
            _root = root;
            _patterns = new List<string> { pattern };
            _option = option;
        }
    
        public FileSystemEnumerable(DirectoryInfo root, IList<string> patterns, SearchOption option)
        {
            _root = root;
            _patterns = patterns;
            _option = option;
        }
    
        public IEnumerator<FileSystemInfo> GetEnumerator()
        {
            if (_root == null || !_root.Exists) yield break;
    
            IEnumerable<FileSystemInfo> matches = new List<FileSystemInfo>();
            try
            {
                _logger.DebugFormat("Attempting to enumerate '{0}'", _root.FullName);
                foreach (var pattern in _patterns)
                {
                    _logger.DebugFormat("Using pattern '{0}'", pattern);
                    matches = matches.Concat(_root.EnumerateDirectories(pattern, SearchOption.TopDirectoryOnly))
                                     .Concat(_root.EnumerateFiles(pattern, SearchOption.TopDirectoryOnly));
                }
            }
            catch (UnauthorizedAccessException)
            {
                _logger.WarnFormat("Unable to access '{0}'. Skipping...", _root.FullName);
                yield break;
            }
            catch (PathTooLongException ptle)
            {
                _logger.Warn(string.Format(@"Could not process path '{0}\{1}'.", _root.Parent.FullName, _root.Name), ptle);
                yield break;
            } catch (System.IO.IOException e)
            {
                // "The symbolic link cannot be followed because its type is disabled."
                // "The specified network name is no longer available."
                _logger.Warn(string.Format(@"Could not process path (check SymlinkEvaluation rules)'{0}\{1}'.", _root.Parent.FullName, _root.Name), e);
                yield break;
            }
    
    
            _logger.DebugFormat("Returning all objects that match the pattern(s) '{0}'", string.Join(",", _patterns));
            foreach (var file in matches)
            {
                yield return file;
            }
    
            if (_option == SearchOption.AllDirectories)
            {
                _logger.DebugFormat("Enumerating all child directories.");
                foreach (var dir in _root.EnumerateDirectories("*", SearchOption.TopDirectoryOnly))
                {
                    _logger.DebugFormat("Enumerating '{0}'", dir.FullName);
                    var fileSystemInfos = new FileSystemEnumerable(dir, _patterns, _option);
                    foreach (var match in fileSystemInfos)
                    {
                        yield return match;
                    }
                }
            }
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    

    The usage is fairly simple.

    //This code is public domain
    var root = new DirectoryInfo(@"c:\wherever");
    var searchPattern = @"*.txt";
    var searchOption = SearchOption.AllDirectories;
    var enumerable = new FileSystemEnumerable(root, searchPattern, searchOption);
    

    People are free to use it if they find it useful.

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

Sidebar

Related Questions

Ok I had asked a question recently based on this. But I need to
I have recently had need to checkout an apache project to do some fact-finding/debugging
I have recently had several situations where I need different data from the same
I have recently had a linux server compromised from bots uploading .php scripts and
Using PHP / MySQL all encoded up as UTF, we have recently had to
I have recently upgraded from websphere 5.1.2 to websphere 6.1 server. I had some
I have a project hosted with git. I just recently had to reinstall ubuntu,
So far I have been creating Web Portal but recently I had a request
I have recently been working on implementing an ajax based file uploader for my
I have a project that I have recently started working on seriously but had

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.