vb.net - Get all the folders and sub-folders in side a directory -


i learning vb.net, know how folders , sub-folder s inside directory , how add them listbox. list folders while scanning showing current folders found. have tried few things never seem work. have tried this:

sub getdirectories(byval startpath string, byref directorylist arraylist)     dim dirs() string = directory.getdirectories(startpath)     directorylist.addrange(dirs)     each dir string in dirs         getdirectories(dir, directorylist)     next     each item in directorylist         listbox1.items.add(item)     next end sub  private sub button1_click(sender object, e eventargs) handles button1.click     dim dirlist new arraylist     getdirectories("c:\hexing\", dirlist) end sub 

try this

private sub button1_click(sender object, e eventargs) handles button1.click try     dim dirlist new arraylist     dim dirs() string = directory.getdirectories(startpath)     dirlist.addrange(dirs)     each dir string in dirs         getdirectories(dir, directorylist)     next     catch ex exception end try end sub 

(or)

private sub button1_click(sender object, e eventargs) handles button1.click         each dir string in directory.getdirectories("c:\program files")             listbox1.items.add(dir)         next end sub 

edit

according vb.net 05, list folder, subfolders, , sub subfolders:

the efficient way use recursivity:

 private function getallfolders(byval directory string) string()         'create object         dim fi new io.directoryinfo(directory)         'array store paths         dim path() string = {}         'loop through subfolders         each subfolder io.directoryinfo in fi.getdirectories()             'add folders name             array.resize(path, path.length + 1)             path(path.length - 1) = subfolder.fullname             'recall function each subdirectory             each s string in getallfolders(subfolder.fullname)                 array.resize(path, path.length + 1)                 path(path.length - 1) = s             next         next         return path  end function 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -