fileNum = 0;//该变量不要放到方法体,不然每次递归都变成0
public int GetFileNum(string srcPath)
{
try
{
// 根据源目录的路径得到源目录中包含文件和目录路径的一个字符串数组
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;
}