views:

735

answers:

2

Does anyone know of a faster way to enumerate through a directory and sub-folders to gather all the files in the enumeration? This is what I have right now:

Public Shared allFiles() As String
allFiles = Directory.GetFiles(<ServerLocation>, "*.*", SearchOption.AllDirectories)

Thanks! JFV

EDIT: I am enumerating these files from a server location. I don't know if that will change the perspective of this question or not. Thanks for all the input so far!

+2  A: 

Short answer:

If this code is functionally correct for your project and you haven't proved it to be a problem with a profiler then don't change it. Continue to use a functionally correct solution until you prove it to be slow.

Long answer:

How fast or slow this particular piece of code is depends on a lot of factors. Many of which will depend on the specific machine you are running on (for instance hard drive speed). Looking at code that involves the file system and nothing else, it's very difficult to say "x is faster than y" with any degree of certainty.

In this case, I can only really comment on one thing. The return type of this method is an array of FileInfo values. Arrays require contiguous memory and very large arrays can cause fragmentation issues in your heap. If you have extremely large directories that you are reading it could lead to heap fragmentiation and indirectly performance issues.

If that turns out to be a problem then you can PInvoke into FindFirstFile / FindNextFile and get them one at a time. The result will be likely functionally slower in CPU cycles but will have less memory pressure.

But I must stress that you should prove these are problems before you fix them.

JaredPar
+1 Much better expressed than I could
Preet Sangha
Yet at the same time, anyone who has tried to list all files in a large directory with .NET will know that it IS a performance problem. Listing 1000 folder names, JUST THE FOLDER NAMES, can take almost a full second.
Daniel Straight
A: 

This is a crude way of doing it.

dir /s /b

Get the output of this into a text file, read it & split by \r\n.
Run the above command in a specific directory, to see if it helps.

To only get the directories

dir /s /b /ad

To only get the files

dir /s /b /a-d

EDIT: Jared is right in saying not to use other approaches unless your approach is proved slow.

shahkalpesh
Could you please clarify why you think invoking dir and reading the output would be faster than a Directory.GetFiles call?
Marek