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

前端 未结 26 2863
逝去的感伤
逝去的感伤 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:57

    For .NET 4.0 and later,

    var files = Directory.EnumerateFiles("C:\\path", "*.*", SearchOption.AllDirectories)
                .Where(s => s.EndsWith(".mp3") || s.EndsWith(".jpg"));
    

    For earlier versions of .NET,

    var files = Directory.GetFiles("C:\\path", "*.*", SearchOption.AllDirectories)
                .Where(s => s.EndsWith(".mp3") || s.EndsWith(".jpg"));
    

    edit: Please read the comments. The improvement that Paul Farry suggests, and the memory/performance issue that Christian.K points out are both very important.

提交回复
热议问题