Directory.GetFiles Progress Report

Is there a quick way to download the full path to each file (to which you have read access), starting with the root directory provided, for example string[]?

I saw System.IO.Directory.GetFiles, but you need to provide a filter before you are allowed to enter SearchOption.AllDirectories, so I tried:

string[] directoryList = Directory.GetFiles(RootPath, "*.*",
                                            SearchOption.AllDirectories);

Where RootPathis my valid root directory. This seemed to work fine, except that it hung for a very long time in a Windows application, pointing to a large directory structure / file size of 29 GB and several hundred thousand files.

So, I was thinking about streaming through BackgroundWorkerso that my GUI would not be blocked, but I'm not sure how to report progress for something like that ProgressBar, since it is just one operator that performs the download, and I have no information about the total number of files up or the number that he has already processed, etc.

+5
source share
1 answer

With Directory.GetFilesyou will always block until all the results are completely processed, which can take a lot of time in a large directory structure.

You can use Directory.EnumerateFiles instead Directory.GetFiles. This returns the results as IEnumerable<string>instead of an array, so you can immediately start processing (or reporting) the results.

Directory.GetFiles, SearchOption.AllDirectories. Directory.GetDirectories .

+8

All Articles