How to add Folders and SubFolders in TreeView


The following code helps you to get folders and subfolders in a treeview
 
Imports System.IO
 
 Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        Dim FBD As New FolderBrowserDialog
        FBD.ShowDialog()
        txtSelectedPath.Text = FBD.SelectedPath.ToString
        tvFoldernFiles.Nodes.Add(txtSelectedPath.Text.ToString, txtSelectedPath.Text)
        GetFilesnFolders(txtSelectedPath.Text.ToString, tvFoldernFiles.Nodes(txtSelectedPath.Text))
        Dim a As String() = tvFoldernFiles.TopNode.Text.Split("".ToCharArray)
        tvFoldernFiles.TopNode.Text = a(a.Length – 1).ToString.ToUpper
    End Sub
 
 
    Public Sub GetFilesnFolders(ByVal FolderName As String, ByVal RefNode As TreeNode)
        Dim FolderList As Array = Directory.GetDirectories(FolderName.ToString)
        Dim FileListofRoot As Array = Directory.GetFiles(FolderName.ToString)
        For int As Integer = 0 To FileListofRoot.Length – 1
            Dim RootFile As New TreeNode(FileListofRoot(int).ToString)
            RefNode.Nodes.Add(RootFile)
        Next
        For i As Integer = 0 To FolderList.Length – 1
            Dim TempNode As New TreeNode(FolderList(i).ToString.ToUpper)
            RefNode.Nodes.Add(TempNode)
            Dim FileList As Array = Directory.GetFiles(FolderList(i).ToString)
            GetFilesnFolders(FolderList(i), TempNode)
        Next
    End Sub
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: