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
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
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
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
- Posted in: VB.NET 2005
Recent Comments