Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse My.Computer.FileSystem to recursively list the files in a directory subtree in Visual Basic 2005
DescriptionThis example shows how to use My.Computer.FileSystem to recursively list the files in a directory subtree in Visual Basic 2005.
KeywordsMy.Computer.FileSystem, directory, directory subtree, Visual Basic 2005, VB.NET, search, files
Categories, Files and Directories
 
Thanks to Joshua Mcfarland.

Subroutine MapDirectory uses My.Computer.FileSystem.GetFiles to list the files in a directory. It then uses My.Computer.FileSystem.GetDirectories to list the directories in that one and recursively calls itself to search them.

 
' Search the directory subtree and make a list of the files.
' (In a real application, you would probably do something
' with the files other than putting them in a Listbox.)
Sub MapDirectory(ByVal dir As String, ByVal lst As ListBox)
    With My.Computer.FileSystem
        ' List this directory's files.
        For Each file As String In .GetFiles(dir)
            'TODO: Do something with the file.
            lstFiles.Items.Add(file)
        Next file

        ' Search child directories.
        For Each folder As Object In .GetDirectories(dir)
            'TODO: Do something with the folder.
            MapDirectory(folder, lst)
        Next folder
    End With
End Sub
 
This example simply lists files in a ListBox. A real application would do something more. For example, it might filter the list, display the file sizes, and so forth.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated