vb.net - folderbrowserdialog Check Space -


i rather new vb , trying figure out how can routine. writing archive program has checklistbox populated directories on drive. user checks on of directories, goes loop grabs directory size , shows on form. issue having once start choosing more 4 or 5 gets slower , slower since reading through checkeditems , validating file size with. there method me grab last item checked or unchecked can add/subtract current size? current code looping through checked items. thank in advance.

dim fsize long = 0 private sub chklstbxworkspace_selectedindexchanged(sender object, e eventargs) handles chklstbxworkspace.selectedindexchanged      dim entry object      if chklstbxworkspace.checkedindices.count > 0         btnstartarchive.enabled = true     else         btnstartarchive.enabled = false     end if     lblworkspacesize.text = chklstbxworkspace.checkedindices.count.tostring & " folders selected."       each entry in chklstbxworkspace.checkeditems         fsize += directorysize("w:\" & entry.tostring, true)         lblworkspacesize.text = chklstbxworkspace.checkedindices.count.tostring & " folders selected.  " & format(fsize, "###,###,###,###,##0") & " bytes."     next     application.doevents() end sub 

you use dictionary store recorded directories , sizes don't have calculate sizes again.

the dictionary collection stores key , value (with key being unique - since you're looking @ folders should hold ok, think aware of). in case key folder name , value folder size.

assuming have form called form1 can declare dictionary so:

imports system.collections.generic  public class form1     dim filesizesdict dictionary(of string, long) = new dictionary(of string, long)() 

note imports of system.collections.generic.

and selectedindexchanged handler like:

private sub chklstbxworkspace_selectedindexchanged(sender object, e eventargs) handles chklstbxworkspace.selectedindexchanged     dim fsize long = 0     dim entry object      if chklstbxworkspace.checkedindices.count > 0         btnstartarchive.enabled = true     else         btnstartarchive.enabled = false     end if     lblworkspacesize.text = chklstbxworkspace.checkedindices.count.tostring & " folders selected."       each entry in chklstbxworkspace.checkeditems         if filesizesdict.containskey(entry.tostring())             fsize += filesizesdict(entry.tostring())         else             dim directorysize 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 note:

  1. the first thing search dictionary see if have calculated size of folder using containskey method
  2. if folder size has been calculated contents dictionary
  3. otherwise, calculate size , store in dictionary future use via add method
  4. i moved lblworkspacesize.text = chklstbxworkspace.checkedindices.count.tostring & " folders selected. " & format(fsize, "###,###,###,###,##0") & " bytes." outside for loop....i not sure of specific use case in case label updated final calculation results; chop , change needed though :-)

caveat: there caveat here in approach in if has added more files (or removed files from) directory has had size calculated won't pick size change not recalculated...i not sure if have significant impact on use case or not worth noting.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -