Directory.GetFiles具有多个过滤器,收集一个字符串数组

时间:2012-11-08 09:41:47

标签: c# asp.net

我想将新项添加到字符串数组中,或者我可以保留在List中。有人可以说明如何做到这一点吗?

 string sourceDir = "C:\\Users\\ozkan\\Desktop\\foto\\"
 string[] picList;        
 string pattern = "*.jpg|*.png|*.gif";
 string[] filters = pattern.Split('|');

 foreach (string filter in filters)
 { 
    // I want to collect all files url in picList
    // Directory.GetFiles returns string array
    picList = Directory.GetFiles(sourceDir, filter);           
 }

1 个答案:

答案 0 :(得分:5)

尝试:

 string sourceDir = "C:\\Users\\ozkan\\Desktop\\foto\\"
 string[] picList;        
 string pattern = "*.jpg|*.png|*.gif";
 string[] filters = pattern.Split('|');
 picList =  filters .SelectMany(f=> Directory.GetFiles(sourceDir , f)).ToArray();