以前写过一个通过计算目录遍历所有文件和子目录的方法来获得某目录下的文件个数,结果发现速度极慢,远远不及系统本身目录属性里边显示的速度。
int fileNum = 0;
/// <summary>
/// 获取某目录下的所有文件(包括子目录下文件)的数量
/// </summary>
/// <param name="srcPath"></param>
/// <returns></returns>
public int GetFileNum(string srcPath)
{
try
{
![](/Images/OutliningIndicators/InBlock.gif)
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);
// 遍历所有的文件和目录
foreach(string file in fileList)
{
// 先当作目录处理如果存在这个目录就重新调用GetFileNum(string srcPath)
if(System.IO.Directory.Exists(file))
GetFileNum(file);
else
fileNum++;
}
}
catch (Exception e)
{
MessageBox.Show (e.ToString());
}
return fileNum;
}
近来发现了一个效率更高的方法,只需遍历目录就能获得文件个数,现分享一下
System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(dirPath);
![](/Images/OutliningIndicators/None.gif)
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;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
近来发现了一个效率更高的方法,只需遍历目录就能获得文件个数,现分享一下
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)