如何取某目录下的文件总数,包含子目录的文件

我只知道可以递归调用Directory.GetFiles("路径").Length的方法,不知有没有更好的办法?

---------------------------------------------------------------

楼主还不行吗?下边是VB.NET代码了,通过测试
----------

Private fileNum As Integer = 0

'获取某目录下的所有文件(包括子目录下文件)的数量
Public Function GetFileNum(ByVal srcPath As String) As Integer
Try
'得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
Dim fileList As String() = System.IO.Directory.GetFileSystemEntries(srcPath)
'遍历所有的文件和目录
For Each file As String In fileList
'先当作目录处理如果存在这个目录就重新调用GetFileNum(string srcPath)
If System.IO.Directory.Exists(file) Then
GetFileNum(file)
Else
System.Math.Min(System.Threading.Interlocked.Increment(fileNum), fileNum - 1)
End If
Next
Catch e As Exception
MessageBox.Show(e.ToString)
End Try
Return fileNum
End Function

Published At
Categories with Web编程
Tagged with
comments powered by Disqus