Quicker (quickest?) way to get number of files in a directory with over 200,000 files

前端 未结 10 1438
Happy的楠姐
Happy的楠姐 2021-02-04 07:35

I have some directories containing test data, typically over 200,000 small (~4k) files per directory.

I am using the following C# code to get the number of files in a di

10条回答
  •  星月不相逢
    2021-02-04 08:06

    If you just need a file count, I found that using 'EnumerateFiles()' is much quicker than using 'GetFiles()':

    /*
    String[] Files = Directory.GetFiles(sPath, "*.*", SearchOption.AllDirectories);
    Int32 nCount = Files.Length;
    */
    Int32 nCount = 0;
    var MyFiles = Directory.EnumerateFiles(sPath, "*.*", SearchOption.AllDirectories);
    foreach (String sFile in MyFiles) nCount++;
    Console.WriteLine("File Count: {0}", nCount);
    

提交回复
热议问题