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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:33:18+00:00 2026-05-26T03:33:18+00:00

I am trying to enumerate through files on my computer using the below code

  • 0

I am trying to enumerate through files on my computer using the below code but everytime it hits a file or dir that I don’t have permission to read it throws an exception. Is there any way I can continue searching after the exception has been thrown? I know some people have had similar issues but is there any other way of doing this other than checking every file/folder individually?

try
{
    string[] files = Directory.GetFiles(@"C:\", "*.*",SearchOption.AllDirectories);
    foreach (string file in files)
    {
        Console.WriteLine(file);
    }
}
catch
{
}

Thanks for any help as this is driving me mad!

  • 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-05-26T03:33:18+00:00Added an answer on May 26, 2026 at 3:33 am

    I came across the same problem just today. I hacked together the following code. If you want to use it in a real product you might need to improve the error handling. Since this was for a one-shot script I didn’t care much.

    static IEnumerable<string> EnumerateFilesRecursive(string root,string pattern="*")
    {
        var todo = new Queue<string>();
        todo.Enqueue(root);
        while (todo.Count > 0)
        {
            string dir = todo.Dequeue();
            string[] subdirs = new string[0];
            string[] files = new string[0];
            try
            {
                subdirs = Directory.GetDirectories(dir);
                files = Directory.GetFiles(dir, pattern);
            }
            catch (IOException)
            {
            }
            catch (System.UnauthorizedAccessException)
            {
            }
    
            foreach (string subdir in subdirs)
            {
                todo.Enqueue(subdir);
            }
            foreach (string filename in files)
            {
                yield return filename;
            }
        }
    }
    

    To use it you can either:

    string[] files = EnumerateFilesRecursive(@"C:\").ToArray();//Note the ToArray()
    foreach (string file in files)
    {
       Console.WriteLine(file);
    }
    

    which first enumerates all files, stores all file names in memory and only then displays them. Alternatively you can:

    IEnumerable<string> files = EnumerateFilesRecursive(@"C:\");//Note that there is NO ToArray()
    foreach (string file in files)
    {
       Console.WriteLine(file);
    }
    

    Which writes while enumerating and thus doesn’t need to keep all filenames in memory at the same time.

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

Sidebar

Related Questions

I'm trying to enumerate through all the Controls of a Page, but all I
I'm trying to enumerate files through the Windos 7 library API, e.g. with SHLoadLibraryFromKnownFolder
I've got a curious item, I'm trying to enumerate through an xml file with
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
I trying to write an application that has a collection of students using different
I am trying to use the code below to group employees by department. I
I'm trying to create a master index file for a bunch of HTML files
I'm trying to enumerate the entry points from the browser to JavaScript code. This
I'm trying to make an iterator that performs breadth-first traversal of all the files
I'm trying to create a SharePoint application that logs to ULS using a set

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.