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

前端 未结 26 2451
逝去的感伤
逝去的感伤 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 06:05

    Let

    var set = new HashSet { ".mp3", ".jpg" };
    

    Then

    Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
             .Where(f => set.Contains(
                 new FileInfo(f).Extension,
                 StringComparer.OrdinalIgnoreCase));
    

    or

    from file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
    from ext in set
    where String.Equals(ext, new FileInfo(file).Extension, StringComparison.OrdinalIgnoreCase)
    select file;
    

提交回复
热议问题