Hello...

I have created application (duplicate file detector) and now I have a progress bar and I want synchronize "how many percentage of scan is ready"

Here is my code for scanning:

private void scanFiles(string rootDirectory)
        {
            try
            {
                if (stop)
                {
                    return;
                }
                
                foreach (string dir in Directory.GetDirectories(rootDirectory))
                {
                    if (stop)
                    {
                        return;
                    }

                    Application.DoEvents();
                    if (dir.ToLower().IndexOf("$recycle.bin") == -1)
                    {
                        scanFiles(dir);
                    }
                }

                foreach (string file in Directory.GetFiles(rootDirectory))
                {
                    if (stop)
                    {
                        return;
                    }

                    Application.DoEvents();

                    // Here is Code for comparing...
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

So... how to set progress bar to show "how many percentage of scan is ready" ?

Thanks...

Recommended Answers

All 5 Replies

if you want it to be accurate, you need to calculate up front the total items to be processed (scanned?), then divide into total items already processed * 100....

Here is an example of using BackgroundWorker with file copies in which the total number of bytes of all files to be copied are determined up front, then progress determined by bytes processed thus far: http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/a285bdb9-6838-48f3-b8ed-1bf0156f8c51/ Scroll down to the 1st post/reply by "NoBugs"

Here is an excerpt from that post where the author both: 1) builds list of files to process, and 2) calculates number of bytes that will be processed:

// Create list of files to copy
      string[] theExtensions = { "*.jpg", "*.jpeg", "*.bmp", "*.png", "*.gif" };
      List<FileInfo> files = new List<FileInfo>();
      string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
      DirectoryInfo dir = new DirectoryInfo(path);
      long maxbytes = 0;
      foreach (string ext in theExtensions) {
        FileInfo[] folder = dir.GetFiles(ext, SearchOption.AllDirectories);
        foreach (FileInfo file in folder) {
          if ((file.Attributes & FileAttributes.Directory) != 0) continue;
          files.Add(file);
          maxbytes += file.Length;
        }
      }

If you just wanted a rough progress, you could use progressBar1.Maximum = Directory.GetDirectories(rootDirectory).Length; to find the number of directories to check. Then then increment the progress bar's value after each directory is checked.

It wont be nearly as accurate as DdoubleD's method, but it is simpler to code and gives a general idea. If it hits a large directory there will be a long wait between increments.

If you can implement DdoubleD's way then do so, it will give your user a much better feel for how far along the process is. Just thought i'd throw in an alternative :)

Instead of reading the bytes and doing the calculation, or by directory, why now go half way, just count all the files to be scanned, then increment it as it finishes files...

DdoubleD's way would be the most accurate, and Ryshad's way would be the fastest, Just counting all the files would be significantly faster than the first method, but more accurate than the second.

Guess its all up to your taste. Speed or accuracy.

Everyone dissed on the file copy and transfer time remaining calculations on XP, so Vista and 7 spend more time calculating the time it will take. Now the time remaining estimation is significantly more accurate, but it takes extra time calculating instead of transferring files, so its actually slower to copy or move files now thanks to that accuracy calculations. - a person like myself, cares about how long it will take only because I wish it would go faster.

Yep, its all about finding the balance between performance and precision and the windows time estimates are a classic example. Everyone has seen installation processes that sit at one percentage for an age then leap through the rest in seconds.
As diamonddrake said, it all comes down to how accurate/how fast you need it to be :)

Hey guys, sorry for my delay... Thank you for your assistance, I fixed my problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.