The tree generation is started by clicking a button called btnRead. I also have a control on the form called tvFolders (TreeView Folders).
Imports System.IO
Private Sub BuildFolderTree(ByVal frmNode As Tree, ByVal curPath As String)
Dim curDirInfo As DirectoryInfo
Dim newFolder As TreeNode
Dim curFolder As String
Dim subFolder As String
Try
For Each curFolder In My.Computer.FileSystem.GetDirectories(curPath)
subFolder = My.Computer.FileSystem.GetDirectories(curFolder)
curDirInfo = My.Computer.FileSystem.GetDirectoryInfo(curFolder)
If Not (curDirInfo.Attributes = 22 Or curDirInfo.Attributes = FileAttributes.Hidden) Then
If frmNode Is Nothing Then
newFolder = tvFolders.Nodes.Add(subFolder)
Else
newFolder = frmNode.Nodes.Add(subFolder)
End If
BuildFolderTree(newFolder, My.Computer.FileSystem.CombinePath(curPath, subFolder))
End If
Next curFolder
Catch curEx As Security.SecurityException
' You can put a warning message here or just ignore it.
Catch curEx As UnauthorizedAccessException
' You can put a warning message here or just ignore it.
System.Windows.Forms.Application.DoEvents()
End Sub
This code shows also how Recursive Programming works. In order for the tree to be built, the program has to recursively go through each folder and check and see if it has sub folders. If it does a tree has to be built for that subfolder as well. This is the reason for having a BuildFolderTree sub procedure call in the middle of the sub procedure.
One Part I am not sure about is the curDirInfo.Attributes = 22. I did some research to find out why the attribute is equal to 22 but I could not find anything about a Folder Attribute equaling 22. The list of folder Attributes are as follows and doesn't include a 22.
Normal 0
ReadOnly 1
Hidden 2
System 4
Volume 8
Directory 16
Archive 32
Alias 64
Compressed 128