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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:20:22+00:00 2026-06-11T11:20:22+00:00

I previously asked the question Get all files and directories in specific path fast

  • 0

I previously asked the question Get all files and directories in specific path fast in order to find files as fastest as possible. I am using that solution in order to find the file names that match a regular expression.

I was hoping to show a progress bar because with some really large and slow hard drives it still takes about 1 minute to execute. That solution I posted on the other link does not enable me to know how many more files are missing to be traversed in order for me to show a progress bar.

One solution that I was thinking about doing was trying to obtain the size of the directory that I was planing traversing. For example when I right click on the folder C:\Users I am able to get an estimate of how big that directory is. If I am able to know the size then I will be able to show the progress by adding the size of every file that I find. In other words the progress = (current sum of file sizes) / directory size

For some reason I have not been able to efficiently get the size of that directory.

Some of the questions on stack overflow use the following approach:

enter image description here

But note that I get an exception and are not able to enumerate the files. I am curios in trying that method on my c drive.

On that picture I was trying to count the number of files in order to show a progress. I will probably not going to be able to get the number of files efficiently using that approach. I where just trying some of the answers on stack overflow when people asked how to get the number of files on a directory and also people asked how the get the size f a directory.

  • 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-11T11:20:23+00:00Added an answer on June 11, 2026 at 11:20 am

    Solving this is going to leave you with one of a few possibilities…

    1. Not displaying a progress
    2. Using an up-front cost to compute (like Windows)
    3. Performing the operation while computing the cost

    If the speed is that important and you expect large directory trees I would lean to the last of these options. I’ve added an answer on the linked question Get all files and directories in specific path fast that demonstrates a faster means of counting files and sizes than you are currently using. To combine this into a multi-threaded piece of code for option #3, the following can be performed…

    static void Main()
    {
        const string directory = @"C:\Program Files";
        // Create an enumeration of the files we will want to process that simply accumulates these values...
        long total = 0;
        var fcounter = new CSharpTest.Net.IO.FindFile(directory, "*", true, true, true);
        fcounter.RaiseOnAccessDenied = false;
        fcounter.FileFound +=
            (o, e) =>
                {
                    if (!e.IsDirectory)
                    {
                        Interlocked.Increment(ref total);
                    }
                };
    
        // Start a high-priority thread to perform the accumulation
        Thread t = new Thread(fcounter.Find)
            {
                IsBackground = true, 
                Priority = ThreadPriority.AboveNormal, 
                Name = "file enum"
            };
        t.Start();
    
        // Allow the accumulator thread to get a head-start on us
        do { Thread.Sleep(100); }
        while (total < 100 && t.IsAlive);
    
        // Now we can process the files normally and update a percentage
        long count = 0, percentage = 0;
        var task = new CSharpTest.Net.IO.FindFile(directory, "*", true, true, true);
        task.RaiseOnAccessDenied = false;
        task.FileFound +=
            (o, e) =>
                {
                    if (!e.IsDirectory)
                    {
                        ProcessFile(e.FullPath);
                        // Update the percentage complete...
                        long progress = ++count * 100 / Interlocked.Read(ref total);
                        if (progress > percentage && progress <= 100)
                        {
                            percentage = progress;
                            Console.WriteLine("{0}% complete.", percentage);
                        }
                    }
                };
    
        task.Find();
    }
    

    The FindFile class implementation can be found at FindFile.cs.

    Depending on how expensive your file-processing task is (the ProcessFile function above) you should see a very clean progression of the progress on large volumes of files. If your file-processing is extremely fast, you may want to increase the lag between the start of enumeration and start of processing.

    The event argument is of type FindFile.FileFoundEventArgs and is a mutable class so be sure you don’t keep a reference to the event argument as it’s values will change.

    Ideally you will want to add error handling and probably the ability to abort both enumerations. Aborting the enumeration can be done by setting “CancelEnumeration” on the event argument.

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

Sidebar

Related Questions

I asked a question previously to get a UCS-2/HexEncoded string from UTF-8, and I
I previously asked this question here: Stop user from using enter to pass a
In a previous question I asked how I would get a Customers first Order,
I had previously asked this question, trying to get started with this code: The
I previously asked a question about fetching the last 100 mentions for a person
I previously asked a question about chaining conditions in Linq To Entities. Now I
Hi guys i previously asked a question and got a good solution. here is
I've previously asked a question related to generating a random unsigned char and pretty
I know this question was asked previously and the reply was to override onPrepareContextMenu()/onCreateContextMenu().
This is an extension of a question I previously asked here . Long story

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.