Results 1 to 3 of 3

Thread: [RESOLVED] Getting Directory size

  1. #1

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Resolved [RESOLVED] Getting Directory size

    How can i get the total size of all files in a directory?
    i tried this but had no luck
    Code:
    Dim dDir As IO.DirectoryInfo
                TextBox4.Text = dDir.Attributes.

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Getting Directory size

    http://social.msdn.microsoft.com/for...-5f1be2c0ee4f/

    Code from link above which works but has minor issues which I fixed.

    Code:
    Imports System.IO
    Code:
        Private Overloads Function DirectorySize(ByVal sPath As String, ByVal bRecursive As Boolean) As Long
            Dim lngNumberOfDirectories As Long = 0
            Dim Size As Long = 0
    
            Try
                Dim fil As FileInfo
                Dim diDir As New DirectoryInfo(sPath)
    
                For Each fil In diDir.GetFiles()
                    Size += fil.Length
                Next fil
    
                If bRecursive = True Then
                    Dim diSubDir As DirectoryInfo
                    For Each diSubDir In diDir.GetDirectories()
                        Size += DirectorySize(diSubDir.FullName, True)
                        lngNumberOfDirectories += 1
                    Next
    
                End If
    
                Return Size
    
            Catch fex As System.IO.FileNotFoundException
    
                ' File not found. Take no action
    
            Catch ex As Exception
                ' Another error occurred
                Return 0
    
            End Try
        End Function

    Code:
    Console.WriteLine(DirectorySize("C:\FolderToCheckSizeOf", True))

  3. #3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width