前些天,为了搞一个项目,需要计算目录大小,这下可把我弄惨了哦!在网上到处搜集资料什么CSDN
MSDN我全跑遍了,原来NET下没有直接计算目录大小 怎么办????
可谓 工夫不负有心人啊!终于在那个网站上找到了关于计算目录下所有文件大小的资料,计算目录下所
有文件大小就相当与计算目录大小啥!所以问题解决了
下边是VB。NET代码
Public
Shared
Function ListFolderTotalSize()
Function ListFolderTotalSize(ByVal userFolder As String) As Long
Dim fileSizemain As Long = 0
Dim mainDir As DirectoryInfo = New DirectoryInfo(userFolder)
Dim Finfosmain() As FileInfo = mainDir.GetFiles()
Dim f As FileInfo
For Each f In Finfosmain
fileSizemain += f.Length
Next
Dim Dinfos() As DirectoryInfo = mainDir.GetDirectories()
Dim i As Integer
For i = 0 To Dinfos.Length- 1 Step i + 1
fileSizemain += ListFolderTotalSize(Dinfos(i).FullName)
Next
Return fileSizemain
End Function
Dim fileSizemain As Long = 0
Dim mainDir As DirectoryInfo = New DirectoryInfo(userFolder)
Dim Finfosmain() As FileInfo = mainDir.GetFiles()
Dim f As FileInfo
For Each f In Finfosmain
fileSizemain += f.Length
Next
Dim Dinfos() As DirectoryInfo = mainDir.GetDirectories()
Dim i As Integer
For i = 0 To Dinfos.Length- 1 Step i + 1
fileSizemain += ListFolderTotalSize(Dinfos(i).FullName)
Next
Return fileSizemain
End Function
使用ListFolderTotalSize("目录名")直接调用,对了得到的值为字节要想得到需要的效果,请自行计算!