Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a listview with 2 colums (Folder Path & Size). In the listview i want to be able to enter in a folder path in the directory colum and then determine the size of the directory and sub directorys...

If anyone is able to assist in this much would be appericated....i have been suggest code that works....but how do i apply that to a listview... i no a for next will be need.

Function GetFolderSize(ByVal DirPath As String, _
   Optional IncludeSubFolders as Boolean = True) As Long

  Dim lngDirSize As Long
  Dim objFileInfo As FileInfo
  Dim objDir As DirectoryInfo = New DirectoryInfo(DirPath)
  Dim objSubFolder As DirectoryInfo

Try
 
'add length of each file
  For Each objFileInfo In objDir.GetFiles()
    lngDirSize += objFileInfo.Length
  Next

  'call recursively to get sub folders
  'if you don't want this set optional
  'parameter to false 
If IncludeSubFolders then
  For Each objSubFolder In objDir.GetDirectories()
    lngDirSize += GetFolderSize(objSubFolder.FullName)
  Next
End if

Catch Ex As Exception
 

End Try

   Return lngDirSize
End Function
Posted

1 solution

Well, you just need to use the GetDirectories method of the DirectoryInfo object to get an array of all directories in the current directory. The you can just add to the listview calling GetFolderSize for each directory in the array, something like this:-

VB
Public Sub LoadDirectoriesInListView(ByVal currentDirectoryValue As String)

        ListView1.Items.Clear()
        currentDirectory = currentDirectoryValue
        Dim currentDirectoryInfo As DirectoryInfo = New DirectoryInfo(currentDirectory)
        Dim directoryArray As DirectoryInfo() = currentDirectoryInfo.GetDirectories()
        Dim dir As DirectoryInfo
        For Each dir In directoryArray
            Dim lvi As New ListViewItem(dir.Name)
            lvi.SubItems.Add(GetFolderSize(dir.FullName, False))
            ListView1.Items.Add(lvi)
        Next

    End Sub


Then you just call it like this:-

LoadDirectoriesInListView(currentDirectory)


Hope this helps
 
Share this answer
 
Comments
SIFNOk 15-Sep-11 7:09am    
Thanks For Your Reply Wayne...
Nice codeee but i am unable to define "GetFolderSize"

In line:
lvi.SubItems.Add(GetFolderSize(dir.FullName, False))

Please could u possibly advise.
Wayne Gaylard 15-Sep-11 7:20am    
GetFolderSize is the function you defined in your question above, that returns the size of the folder/directory.
SIFNOk 15-Sep-11 7:58am    
Aaaahhh makes sense nowww! Thanks you sooo much Wayne!

And how would i call this from a timer function...i want to folder sizes to update in the list on da hour.

:)

Wayne Gaylard 15-Sep-11 8:09am    
You could just call the LoadDirectoriesInListView function from your timer tick event.
SIFNOk 15-Sep-11 12:46pm    
Hi Wayne,


Thanks for your reply...the code works great, i was wondering if your could possibly help me with an issue that i may be able apply the same working. But basically i have a listview with 2 colums. Directory name and size. In column 1 - folder name, i want to be able to put a directory location (ie. C:\Windows) and then in coum 2 - size - determine the size of the directory including sub folders.


if you could point me in the right direction or help rewrite the above code to work... i appericate all your help :)


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900