EnumerateFiles Vs GetFiles in .Net 4.5

While getting files from a folder programmatically in .net we use Directory.GetFiles(filePath) api. But as the number of files increases (for ex. 2.5 lakh files) in the folder it takes more time to get all files present in the folder. Now question arises, is there any better way?

Answer is yes. Use Directory.EnumerateFiles(filePath). The difference between is when  you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

Now question is there any proof?

Yes I will show in my sample application

  1. The directory has 250000 (2.5 lakh) files of excel type with size of 15kb.
  2. To create such a larger number of files in a folder, you can do manual copy paste or my sample code for that.

//=================== New File Creator================================

long noOfFiles = 100000000;

string src = @”H:\Test\FileTest\SampleFileTemplate\”;

string srcFileName = “TestExcel”;

string dest = @”H:\Test\FileTest\AllFiles\”;

string temp = @”H:\Test\FileTest\Temp\”;

Parallel.For(0, noOfFiles, i =>

{

var tempFileName = srcFileName + “_” + DateTime.Now.Ticks.ToString() + “_” + i + “.xlsx”;

File.Copy(src + srcFileName + “.xlsx”, temp + tempFileName);

File.Move(temp + tempFileName, dest + tempFileName);

});

Parallel.For(0, noOfFiles, i =>

{

var tempfilename = srcFileName + “_” + Guid.NewGuid().ToString() + “.xlsx”;

File.Copy(src + srcFileName + “.xlsx”, temp + tempfilename);

File.Move(temp + tempfilename, dest + tempfilename);

});

  1.  3. Now run a stop watch to count the time difference between calling Enumerate Files and Get Files. To do that here is my sample code

//=========================== File read performance counter.             string filePath = @”H:\Test\FileTest\AllFiles\”;            Stopwatch w = new Stopwatch();            w.Start();            var enumFiles = Directory.EnumerateFiles(filePath);            w.Stop();            Console.WriteLine(“Directory.EnumerateFiles took ” + w.ElapsedMilliseconds + ”  milliseconds”);            w = new Stopwatch();            w.Start();            var get = Directory.GetFiles(filePath);            w.Stop();            Console.WriteLine(“Directory.GetFiles took ” + w.ElapsedMilliseconds + ”  milliseconds”);

4           4. And finally here is the result

Hence before using GetFiles method analyse the scenario then use correct method for your program.

Happy coding!!!!!

msdn Link

Leave a comment