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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:36:25+00:00 2026-05-24T08:36:25+00:00

I would like to optimize the following method, that returns the total file count

  • 0

I would like to optimize the following method, that returns the total file count of the specified folder and all subfolders, for speed and memory usage; any advice is appreciated.

Thanks.

private int countfiles(string srcdir)
{
    try
    {
        DirectoryInfo dir = new DirectoryInfo(srcdir);

        //if the source dir doesn't exist, throw an exception
        if (!dir.Exists)
            throw new ArgumentException("source dir doesn't exist -> " + srcdir);

        int count = dir.GetFiles().Length;

        //loop through each sub directory in the current dir
        foreach (DirectoryInfo subdir in dir.GetDirectories())
        {
            //recursively call this function over and over again
            count += countfiles(subdir.FullName);
        }

        //cleanup
        dir = null;

        return count;
    }
    catch (Exception exc)
    {
        MessageBox.Show(exc.Message);
        return 0;
    }           
}

So I did some benchmarking with the suggestions that were proposed. Here are my findings:

  • My method, with recursion, is the slowest finding 9062 files in a directory tree in 6.234 seconds.

  • @Matthew’s answer, using SearchOption.AllDirectories, is the fastest finding the same 9062 files in 4.546 seconds

  • @Jeffery’s answer, using LINQ, is in the middle of the pack finding the same 9062 files in 5.562 seconds.

Thank you everyone for your suggestions.

  • 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-24T08:36:27+00:00Added an answer on May 24, 2026 at 8:36 am

    Could you not change the entire method to:

    int count = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Length;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to optimize my scp deployment which currently copies all files to
I have an extension method that looks like the following: //[MethodImpl(MethodImplOptions.NoOptimization)] public static IEnumerable<char>
I would like to optimize following lines of code for Sorting. public ViewResult Index(string
I came across the following code recently and would like to optimize it: Public
I would like to implement an B+ tree in Java and try to optimize
I have built an application in C# that I would like to be optimized
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to test a string containing a path to a file for
I'd like to use the following idiom, that I think is non-standard. I have
I would like some advice about the following Makefile. It works fine, but it

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.