获取某目录下的所有文件(包括子目录下文件)的数量
1
2
int fileNum = 0;
3
/**//// <summary>
4
/// 获取某目录下的所有文件(包括子目录下文件)的数量
5
/// </summary>
6
/// <param name="srcPath"></param>
7
/// <returns></returns>
8
public int GetFileNum(string srcPath)
9
{
10
try
11
{
12
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
13
string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);
14
// 遍历所有的文件和目录
15
foreach(string file in fileList)
16
{
17
// 先当作目录处理如果存在这个目录就重新调用GetFileNum(string srcPath)
18
if(System.IO.Directory.Exists(file))
19
GetFileNum(file);
20
else
21
fileNum++;
22
}
23
24
}
25
catch (Exception e)
26
{
27
MessageBox.Show (e.ToString());
28
}
29
return fileNum;
30
}
31

2
int fileNum = 0;3
/**//// <summary>4
/// 获取某目录下的所有文件(包括子目录下文件)的数量5
/// </summary>6
/// <param name="srcPath"></param>7
/// <returns></returns>8
public int GetFileNum(string srcPath)9
{10
try11
{ 12
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组13
string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);14
// 遍历所有的文件和目录15
foreach(string file in fileList)16
{17
// 先当作目录处理如果存在这个目录就重新调用GetFileNum(string srcPath)18
if(System.IO.Directory.Exists(file))19
GetFileNum(file);20
else21
fileNum++;22
}23
24
}25
catch (Exception e)26
{27
MessageBox.Show (e.ToString());28
}29
return fileNum;30
}31



浙公网安备 33010602011771号