使用 Directory.GetFiles 进行多个过滤器?

发布于 2024-10-10 07:14:02 字数 213 浏览 0 评论 0原文

我正在尝试通过 Directory.GetFiles() 命令使用多个过滤器。

假设我想匹配 .html 和 .css 文件。我正在使用这个:

Directory.GetFiles(path,"*.html|*.css");

我没有看到任何文档表明这是受支持的,并且它最终与 HTML 或 CSS 文件不匹配。我有什么遗漏的吗?

I'm trying to use multiple filters with the Directory.GetFiles() command.

So say I want to match both .html and .css files. I'm using this:

Directory.GetFiles(path,"*.html|*.css");

I don't see any documentation however that this is supported, and it ends up not matching either HTML or CSS files. Is there something I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫南 2024-10-17 07:14:02

Directory.GetFiles 函数不支持多个筛选器。我的解决方案:

string patter = "*.jpg|*.png|*.gif";
string[] filters = patter.Split('|');
foreach(string filter in filters )
{
  // call Directory.GetFiles(path, filter) here;
}

The Directory.GetFiles function doesn't support multiple filters. My solution:

string patter = "*.jpg|*.png|*.gif";
string[] filters = patter.Split('|');
foreach(string filter in filters )
{
  // call Directory.GetFiles(path, filter) here;
}
不知在何时 2024-10-17 07:14:02

还有一个下降解决方案可以避免 foreach 循环(在 Linq 的帮助下):

string[] filters = new[]{"*.jpg", "*.png", "*.gif"};
string[] filePaths = filters.SelectMany(f => Directory.GetFiles(basePath, f)).ToArray();

There is also a descent solution which avoids foreach loops (with the help of Linq):

string[] filters = new[]{"*.jpg", "*.png", "*.gif"};
string[] filePaths = filters.SelectMany(f => Directory.GetFiles(basePath, f)).ToArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文