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

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

问题描述

我正在尝试使用 Directory.GetFiles() 方法来检索多种类型的文件列表,例如 mp3jpg的.我已经尝试了以下两种方法,但都没有运气:

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"));

请阅读评论.Paul 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天全站免登陆