c#获取文件夹指定的多种类型的文件

349 阅读1分钟
//遍历本文件夹中的文件及子文件夹中的文件:
/*newpath = Directory.GetFiles(newsecpath, "*.*",
                    SearchOption.AllDirectories).Where(s => s.EndsWith(".png") || s.EndsWith(".jpg")
                    || s.EndsWith(".ico")).ToArray();*/
//仅遍历本文件夹中的文件:
newpath = Directory.GetFiles(newsecpath, "*.*",
                    SearchOption.TopDirectoryOnly).Where(s => s.EndsWith(".png") || s.EndsWith(".jpg")
                    || s.EndsWith(".ico")).ToArray();

其中,newsecpath为文件夹名称,以上过滤出了.png、.jpg、.ico并存到字符串数组newpath。