You need to recursively traverse a directory tree and identify all subdirectory names.
Sample code folder: Chapter 12\IterateDirectories
Recursively call the My.Computer.FileSystem. GetDirectories() method to scan each subdirectory and its contents in order.
This recipe's sample code fills in a TReeView control with all subdirectories and directory descendants of a specified base path.
In a new Windows Forms project, add a TextBox control named StartPath, a Button control named ActTraverse, and a TReeView control named PathTree to Form1. You can add labels and provide meaningful captions if you wish, as is done in Figure 12-3.
Figure 12-3. Controls for the directory traversal sample
Now add the following source code to the form's class template:
Private Sub ActTraverse_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ActTraverse.Click ' ----- Make sure the supplied path is valid. If (My.Computer. FileSystem.DirectoryExists( _ StartPath.Text) = False) Then MsgBox("Please supply a valid directory path.", _ MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, _ "Invalid Path") Exit Sub End If ' ----- Clear any previous tree. PathTree.Nodes.Clear( ) ' ----- Call the scanning routine, a recursive routine. BuildDirectoryTree(Nothing, StartPath.Text) End Sub Private Sub BuildDirectoryTree(ByVal fromNode As TreeNode, _ ByVal basePath As String) ' ----- Attach all of the subdirectories found in ' basePath to the supplied node. If fromNode is ' Nothing, create root entries. Dim newDirectory As TreeNode Dim justTheSubdirectory As String ' ----- Retrieve all directories in this path. For Each oneDirectory As String In _ My.Computer. FileSystem.GetDirectories(basePath) ' ----- Extract just the final directory name. justTheSubdirectory = My.Computer.FileSystem.GetName( _ oneDirectory) If (fromNode Is Nothing) Then ' ----- Add a top-level subdirectory. newDirectory = PathTree.Nodes.Add( _ justTheSubdirectory) Else ' ----- Add a subordinate node. newDirectory = fromNode.Nodes.Add( _ justTheSubdirectory) End If ' ----- Recurse into the subdirectory. BuildDirectoryTree(newDirectory, My.Computer.FileSystem. _ CombinePath(basePath, justTheSubdirectory)) Next oneDirectory End Sub
To use the program, type a valid directory path into the StartPath field, then click ActTraverse to build the subdirectory tree structure. Figure 12-4 shows this program traversing the Visual Studio installation directory.
Figure 12-4. Iteration of a directory ("Common7" expanded after traversal)
This code uses several of the path-manipulation features found in the My.Computer.FileSystem object, including the GetDirectories() method, which returns a list of subdirectory path strings within the supplied parent directory.
Because you cannot know in advance how deep the nesting is for subdirectories, you can't hardcode a specific limit into the routine. By using a recursive functiona function that calls itselfyou can effectively nest to any depth required. BuildDirectoryTree() adds a list of subdirectories in a base parent directory to the TReeView control. When it encounters a directory, it first adds it to the treeView control and then calls itself, using the just-added subdirectory as the new base path. That call adds all sub-subdirectories to the just-added subdirectory node. Each of those sub-subdirectories, in turn, calls BuildDirectoryTree() yet again to attach its own nested directories. And on it goes, until BuildDirectoryTree() reaches a directory with no child directories. At that point, the innermost call to BuildDirectoryTree() exits, returning to the previous call. As each level runs out of subdirectories, control is returned up the call stack until the code returns to the initial ActTraverse_Click event handler.
For information on parsing file and directory paths, see Recipe 12.7.