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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:34:13+00:00 2026-05-11T07:34:13+00:00

This earlier SO question talks about how to retrieve all files in a directory

  • 0

This earlier SO question talks about how to retrieve all files in a directory tree that match one of multiple extensions.

eg. Retrieve all files within C:\ and all subdirectories, matching *.log, *.txt, *.dat.

The accepted answer was this:

var files = Directory.GetFiles('C:\\path', '*.*', SearchOption.AllDirectories)             .Where(s => s.EndsWith('.mp3') || s.EndsWith('.jpg')); 

This strikes me as being quite inefficient. If you were searching on a directory tree that contains thousands of files (it uses SearchOption.AllDirectories), every single file in the specified directory tree is loaded into memory, and only then are mismatches removed. (Reminds me of the ‘paging’ offered by ASP.NET datagrids.)

Unfortunately the standard System.IO.DirectoryInfo.GetFiles method only accepts one filter at a time.

It could be just my lack of Linq knowledge, is it actually inefficient in the way I mention?

Secondly, is there a more efficient way to do it both with and without Linq (without resorting to multiple calls to GetFiles)?

  • 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. 2026-05-11T07:34:13+00:00Added an answer on May 11, 2026 at 7:34 am

    I shared your problem and I found the solution in Matthew Podwysocki’s excellent post at codebetter.com.

    He implemented a solution using native methods that allows you to provide a predicate into his GetFiles implementation. Additionally he implemented his solution using yield statements effectively reducing the memory utilization per file to an absolute minimum.

    With his code you can write something like the following:

    var allowedExtensions = new HashSet<string> { '.jpg', '.mp3' };  var files = GetFiles(     'C:\\path',      SearchOption.AllDirectories,      fn => allowedExtensions.Contains(Path.GetExtension(fn)) ); 

    And the files variable will point to an enumerator that returns the files matched (delayed execution style).

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

Sidebar

Ask A Question

Stats

  • Questions 65k
  • Answers 65k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Jeff Yates is right, this is a duplicate to Why… May 11, 2026 at 11:11 am
  • added an answer I would do a global search and replace from =CustomFunction… May 11, 2026 at 11:11 am
  • added an answer Make sure that the :ensure_application_is_installed_by_facebook_user and/or :ensure_authenticated_to_facebook before_filters are not… May 11, 2026 at 11:11 am

Related Questions

Earlier this week I ask a question about filtering out duplicate values in sequence
I had this question earlier and it was concluded it was a bug in
This is related to my earlier question , but I thought I'd simplify it
Earlier I asked this question How to correctly unit test my DAL? , one
This is closely related to an earlier question . In the managed world: How
Earlier I asked why this is considered bad: class Example { public: Example(void); ~Example(void);
This is in reference to a question I asked earlier. Aside from viewing the
This is an attempt to rephrase a question I asked earlier. I'd like to
This pretty much has me defeated. On XP and earlier versions of Windows you
You may think this question is like this question asked on StackOverflow earlier. But

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.