近来发现了一个效率更高的方法,只需遍历目录就能获得文件个数
System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(dirPath);
public static int GetFilesCount(System.IO.DirectoryInfo dirInfo)
{
int totalFile = 0;
totalFile += dirInfo.GetFiles().Length;
foreach (System.IO.DirectoryInfo subdir in dirInfo.GetDirectories())
{
totalFile += GetFilesCount(subdir);
}
return totalFile;
}