Gabriel Nahmias
Gabriel Nahmias

Reputation: 978

A Quicker Directory.GetFiles()

When I run this function, it literally takes minutes sometimes to find only 10 or so files. What is the problem? I created this function:

public static List<string> FindFiles(string sFilename, string sDir, SearchOption soOption = SearchOption.AllDirectories)
{
    List<string> lFiles = new List<string>(Directory.GetFiles(sDir, sFilename, soOption));
    return lFiles;
}

Why is this so slow? Is there another way to accomplish the same thing (finding files in a directory)?

Upvotes: 0

Views: 479

Answers (1)

Michael Bray
Michael Bray

Reputation: 15265

If you are .NET 4+, then you can try Directory.EnumerateFiles instead. It will return faster, but I think the total time that your code runs will still be more or less the same.

Upvotes: 2

Related Questions