环境: C#,VStudio 2013,4.5框架,Winforms
目标:获取与存储在字符串数组中的扩展名匹配的文件夹和子文件夹中的文件数(计数)。扩展名数组可以带有“.”或不带。{" .dat",“txt”,“.msg”}
到目前为止我已经做了什么:当我在扩展名数组中使用"."时,一切正常:{".dat",".txt",".msg"}
我尝试了 Replace,但它总是返回0。
只有当数组中的所有字符串都带有“.”时,代码才有效:
string[] ext= new string[] { ".txt", ".msg", ".dat" };
totalFilesInTN = Directory.EnumerateFiles(dlg1.SelectedPath, "*.*", SearchOption.AllDirectories)
.Count(s => ext.Any(s1 => s1 == Path.GetExtension(s)));
不起作用的代码(始终返回0):
string[] ext= new string[] { "txt", ".msg", "dat" };
totalFilesInTN = Directory.EnumerateFiles(dlg1.SelectedPath, "*.*", SearchOption.AllDirectories)
.Count(s => ext.Any(s1 => s1 == Path.GetExtension(s).Replace(".", "")));
Count
中使用无Where
但谓词相同。 - Tim Schmelter