Multiple filters with Directory.GetFiles?

六月ゝ 毕业季﹏ 提交于 2019-12-11 16:13:35

问题


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?


回答1:


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;
}



回答2:


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


来源:https://stackoverflow.com/questions/4603363/multiple-filters-with-directory-getfiles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!