Improve the performance for enumerating files and folders using .NET

后端 未结 7 1438
一生所求
一生所求 2020-12-08 07:51

I have a base directory that contains several thousand folders. Inside of these folders there can be between 1 and 20 subfolders that contains between 1 and 10 files. I\'d

7条回答
  •  再見小時候
    2020-12-08 08:29

    The method Get1 in above answer (#itsnotalie & #Chibueze Opata) is missing to count the files in the root directory, so it should read:

    private static int Get1(string myBaseDirectory)
    {
        DirectoryInfo dirInfo = new DirectoryInfo(myBaseDirectory);
        return dirInfo.EnumerateDirectories()
                   .AsParallel()
                   .SelectMany(di => di.EnumerateFiles("*.*", SearchOption.AllDirectories))
                   .Count() + dirInfo.EnumerateFiles("*.*", SearchOption.TopDirectoryOnly).Count();
    }
    

提交回复
热议问题