You could probably use a Dictionary to store previously recorded directories and their sizes so you don't have to calculate the sizes again.
The Dictionary is a collection which stores a key and a value (with the key being unique - since you're looking at folders this should hold up OK, I think but it is something to be aware of). In this case your Key is the folder name and the Value will be the folder size.
Assuming I have a Form
called Form1
I can declare my Dictionary like so:
Imports System.Collections.Generic
Public Class Form1
Dim fileSizesDict As Dictionary(Of String, Long) = New Dictionary(Of String, Long)()
Note the imports of System.Collections.Generic
.
And your SelectedIndexChanged
handler could then be something like:
Private Sub chklstbxWorkspace_SelectedIndexChanged(sender As Object, e As EventArgs) Handles chklstbxWorkspace.SelectedIndexChanged
Dim fsize As Long = 0
Dim entry As Object
If chklstbxWorkspace.CheckedIndices.Count > 0 Then
btnStartArchive.Enabled = True
Else
btnStartArchive.Enabled = False
End If
lblWorkspaceSize.Text = chklstbxWorkspace.CheckedIndices.Count.ToString & " folders selected."
For Each entry In chklstbxWorkspace.CheckedItems
If fileSizesDict.ContainsKey(entry.ToString()) Then
fsize += fileSizesDict(entry.ToString())
Else
Dim directorySize As Long = directorySize("w:\" & entry.ToString, True)
fsize += directorySize
fileSizesDict.Add(entry.ToString(), directorySize)
End If
Next
lblWorkspaceSize.Text = chklstbxWorkspace.CheckedIndices.Count.ToString & " folders selected. " & Format(fsize, "###,###,###,###,##0") & " bytes."
End Sub
Couple of things to note:
- The first thing we do is search the Dictionary to see if we have already calculated the size of the folder by using the
ContainsKey
method - If the folder size has been previously calculated then we just get the contents from the Dictionary
- Otherwise, we calculate the size and then store it in the Dictionary for future use via the
Add
method - I moved
lblWorkspaceSize.Text = chklstbxWorkspace.CheckedIndices.Count.ToString & " folders selected. " & Format(fsize, "###,###,###,###,##0") & " bytes."
outside theFor
loop....I am not sure of your specific use case but in this case the Label will just be updated with the final calculation results; chop and change this as needed though :-)
Caveat: There is a caveat here in this approach in that if someone has added more files to (or removed some files from) a directory which has previously had its size calculated then we won't pick up the size change as it is not recalculated...I am not sure if this will have a significant impact on your use case or not but just something worth noting.