Can you call Directory.GetFiles() with multiple filters?

前端 未结 26 2964
逝去的感伤
逝去的感伤 2020-11-22 05:25

I am trying to use the Directory.GetFiles() method to retrieve a list of files of multiple types, such as mp3\'s and jpg\'s. I have t

26条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 05:51

    I wonder why there are so many "solutions" posted?

    If my rookie-understanding on how GetFiles works is right, there are only two options and any of the solutions above can be brought down to these:

    1. GetFiles, then filter: Fast, but a memory killer due to storing overhead untill the filters are applied

    2. Filter while GetFiles: Slower the more filters are set, but low memory usage as no overhead is stored.
      This is explained in one of the above posts with an impressive benchmark: Each filter option causes a seperate GetFile-operation so the same part of the harddrive gets read several times.

    In my opinion Option 1) is better, but using the SearchOption.AllDirectories on folders like C:\ would use huge amounts of memory.
    Therefor i would just make a recursive sub-method that goes through all subfolders using option 1)

    This should cause only 1 GetFiles-operation on each folder and therefor be fast (Option 1), but use only a small amount of memory as the filters are applied afters each subfolders' reading -> overhead is deleted after each subfolder.

    Please correct me if I am wrong. I am as i said quite new to programming but want to gain deeper understanding of things to eventually become good at this :)

提交回复
热议问题