环境:C#,VStudio 2013,4.5框架,Winforms
目标:获取与存储在字符串数组中的扩展名匹配的文件夹和子文件夹中的文件数(Count).扩展数组可以是"." 不是.{ ".DAT", "TXT", "味精"}
到目前为止我做了什么:当我有"." 在扩展数组中,一切正常:{".dat",".txt",".msg"}
我尝试过替换,但它总是返回0.
工作代码(仅当字符串数组中始终带有"."时):
string[] ext= new string[] { ".txt", ".msg", ".dat" };
totalFilesInTN = Directory.EnumerateFiles(dlg1.SelectedPath, "*.*", SearchOption.AllDirectories)
.Count(s => ext.Any(s1 => s1 == Path.GetExtension(s)));
Run Code Online (Sandbox Code Playgroud)
不工作的代码(总是返回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(".", "")));
Run Code Online (Sandbox Code Playgroud)