你可以调用Directory.GetFiles()与多个过滤器? [英] Can you call Directory.GetFiles() with multiple filters?

查看:146
本文介绍了你可以调用Directory.GetFiles()与多个过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 Directory.GetFiles()方法,以获取多种类型的文件,如 MP3 JPG 的。我已经试过这两个的,没有运气如下:

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 tried both of the following with no luck:

Directory.GetFiles("C:\\path", "*.mp3|*.jpg", SearchOption.AllDirectories);
Directory.GetFiles("C:\\path", "*.mp3;*.jpg", SearchOption.AllDirectories);

有没有办法做到这一点在一个电话吗?

Is there a way to do this in one call?

推荐答案

有关.NET 4.0和更高版本,

For .NET 4.0 and later,

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

有关早期版本的.NET中,

For earlier versions of .NET,

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

编辑: 请阅读注释。该保罗Farry 的建议的 Christian.K 指出,都是非常重要的。

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.

这篇关于你可以调用Directory.GetFiles()与多个过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆