5

I use this function, to search for all exe files in selected directory:

public static IEnumerable<string> GetFiles(string root, string searchPattern)
{
    Stack<string> pending = new Stack<string>();
    pending.Push(root);
    while (pending.Count != 0)
    {
        var path = pending.Pop();
        string[] next = null;
        try
        {
            next = Directory.GetFiles(path, searchPattern);
        }
        catch { }
        if (next != null && next.Length != 0)
            foreach (var file in next) yield return file;
        try
        {
            next = Directory.GetDirectories(path);
            foreach (var subdir in next) pending.Push(subdir);
        }
        catch { }
    }
}

How can I update the progress bar status, based on the number of files found?

caitriona
  • 8,569
  • 4
  • 32
  • 36
user1775334
  • 161
  • 1
  • 1
  • 7
  • 2
    Since you don't know the total number of files (or I should say, unless you know the total number of files) knowing how many you've found so far doesn't tell you what % complete you are. Just use a marquee bar. – Servy Oct 25 '12 at 20:00
  • no, its standard windows forms application in VS 2010 – user1775334 Oct 25 '12 at 20:03
  • 1
    The ProgressBar control requires a maximum value, this allows it to increment the UI value correctly. – Jamie Keeling Oct 25 '12 at 20:06
  • If you are using .NET4+, you should be using DirectoryInfo.EnumerateFiles(...) instead of Directory.GetFiles(...). EnumerateFiles allows the list of files to be built as you need items from it and generally more performant. – Matthew Brubaker Oct 25 '12 at 20:31

3 Answers3

1

The point is that you don't know the total number of exe files (aka the 100%) that you'll find so basically you CAN'T render a progress bar! For this kind of tasks it would be more suited an hourglass or a marquee bar...

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159
0

You would want to search through and then set the progressbar maximum to the number of files found.

You can assign a counter that assigns the value a = to # of files found then set

progressBar.Maximum = a;

Pichu
  • 265
  • 2
  • 4
  • 9
  • 2
    The work that he's doing is simply finding the files. He won't know what the maximum is until he's all done. – Servy Oct 25 '12 at 20:03
  • It would require wrapping around twice, finding all the total files to get the maximum and then to actually 'pretend' to searching for total files. His best bet is a marquee bar with a label # of File Found. – Pichu Oct 26 '12 at 20:16
0

Maybe I'm missing something here, but why don't you assign the Maximum of the progress bar to pending.Count and add 1 to the progress bar's value each time you process a file?

Yoinazek
  • 133
  • 5