递归统计指定目录下所有文件容量总和

类别:.NET开发 点击:0 评论:0 推荐:
 

Public Class ShowDirSize

    '递归统计指定目录下所有文件容量总和

    Public Shared Function DirSize(ByVal d As IO.DirectoryInfo) As Long

        Dim Size As Long = 0

        ' 统计本目录下所有文件容量总和

        Dim fis As IO.FileInfo() = d.GetFiles()

        Dim fi As IO.FileInfo

        For Each fi In fis

            Size += fi.Length

        Next fi

        ' 递归统计所有子目录下所有文件容量总和

        Dim dis As IO.DirectoryInfo() = d.GetDirectories()

        Dim di As IO.DirectoryInfo

        For Each di In dis

            Size += DirSize(di)

        Next di

        Return Size

    End Function

End Class


        '调用代码

        Dim dir As New System.IO.DirectoryInfo(TxtDir.Text)

        TxtMessage.Text = ShowDirSize.DirSize(dir)

本文地址:http://com.8s8s.com/it/it43903.htm