הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Thursday, July 13, 2017 8:53 PM | 1 vote
I would like to be able to display the C: drive directory contents in a tree structure using the TreeView Control. I'm trying to use Visual Basic to accomplish this task. I would like to be able to use the "+" symbol next to each directory in order to display the contents/files of each directory or hide the contents/files of each directory.
I'm just wondering if this can be accomplished using Visual Basic in Visual Studio 2010?
The first step that I'm trying to figure out is how do you get the C: drive directory contents to display in a TreeView control on a Visual Basic Form?
Thanks in advance,
Jim
All replies (7)
Friday, July 14, 2017 7:16 AM
Hello,
This forum(Word for Developers) is for development issues related to Word Object Model and your problem is more related to Visual Basic Form, so I would move this thread into Visual Basic.
Thanks for your understanding.
Best Regards,
Terry
Friday, July 14, 2017 8:35 AM | 2 votes
Hi jnhutchi3,
According to your description, you can refer to the code below.
I use one ComboBox and Treeview in the form.
Private Sub Form18_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim drives As DriveInfo() = DriveInfo.GetDrives()
For i As Integer = 0 To drives.Length - 1
ComboBox1.Items.Add(drives(i).Name)
ComboBox1.SelectedIndex = -1
Next
End Sub
Private Sub TreeView1_BeforeExpand(sender As Object, e As TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
Try
Dim courentNode As TreeNode = e.Node
Dim folderPaht As New DirectoryInfo(ComboBox1.SelectedItem + courentNode.FullPath)
For Each dir As DirectoryInfo In folderPaht.GetDirectories()
Dim fileName As String = dir.Name
Dim node As TreeNode = courentNode.Nodes.Add(fileName)
node.Nodes.Add(" ")
Next
For Each file As FileInfo In folderPaht.GetFiles()
Dim ext As String = file.Extension
If ext.ToLower() = ".txt" Then
Dim newNode As TreeNode = courentNode.Nodes.Add(file.Name)
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
TreeView1.Nodes.Clear()
Dim path As New DirectoryInfo(ComboBox1.SelectedItem.ToString())
Try
For Each dir As DirectoryInfo In path.GetDirectories()
Dim node As TreeNode = TreeView1.Nodes.Add(dir.Name)
node.Nodes.Add(" ")
Next
For Each file As FileInfo In path.GetFiles()
If file.Extension.ToLower() = ".txt" Then
Dim node As TreeNode = TreeView1.Nodes.Add(file.Name)
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Best Regards,
Cherry
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Friday, July 14, 2017 7:24 PM | 1 vote
Their are several files and folders that you will not have permissions to iterate through for security reasons so, no you can not list all the files and folders exactly like windows explorer but, you can mimic it with a TreeView pretty close. The link below is for a tutorial i wrote for doing this. It is a bit beyond the beginner level but, perhaps it will help you.
TreeView Drive, Folder, and File Explorer
As seen here, it uses the actual folder and file icons. It will list all the drives on the computer as root directories and you can also add your choice of the special folders as root directories too, as i did with the Desktop and Documents folders in the below image.
If you say it can`t be done then i`ll try it
Friday, July 14, 2017 8:54 PM | 1 vote
The below link has a complete project solution:
https://www.codeproject.com/Articles/301943/TreeView-Explorer-using-VB-NET
Paul ~~~~ Microsoft MVP (Visual Basic)
Monday, July 17, 2017 3:15 AM
Hi,
This is my sample:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.TreeView1.Nodes.Clear() ' -- Clear all nodes
Dim driveLetter As String = Me.TextBox1.Text ' -- get Drive Letter
Me.TreeView1.Nodes.Add(driveLetter)
' Populate this root node
PopulateTreeView(driveLetter & ":\", Me.TreeView1.Nodes(0))
' Expand Parent node
Call Me.prc_Node_Expand_Parent()
MessageBox.Show("TreeView completed!")
End Sub
' PopulateTreeView
Private Sub PopulateTreeView(ByVal driveLetter As String, ByVal parentNode As TreeNode)
SuspendLayout()
Dim folder As String = ""
Try
Dim folders() As String = IO.Directory.GetDirectories(driveLetter)
If (folders.Length <> 0) Then
Dim childNode As TreeNode = Nothing
For Each folder In folders
' ノードを追加
childNode = New TreeNode(folder)
childNode.Text = Me.fnc_Get_LastFolder(childNode.Text)
parentNode.Nodes.Add(childNode)
PopulateTreeView(folder, childNode)
Next
End If
Catch ex As UnauthorizedAccessException
' -- parentNode.Nodes.Add(folder & ": Access Denied")
End Try
End Sub
' get last folder name: exclude parent/prior folder name
Private Function fnc_Get_LastFolder(ByVal childNodeText As String) As String
Dim pos As Integer = childNodeText.LastIndexOf("\")
Dim lastDir As String = childNodeText.Substring(pos + 1, childNodeText.Length - pos - 1)
Return lastDir
End Function
' Expand only Parent node
Public Sub prc_Node_Expand_Parent()
Me.TreeView1.SelectedNode = Me.TreeView1.Nodes(0).Nodes(0)
Me.TreeView1.SelectedNode = Me.TreeView1.Nodes(0)
Me.TreeView1.Nodes(0).EnsureVisible()
End Sub
Ashidacchi
Wednesday, July 19, 2017 7:40 AM
Hi jnhutchi3,
Please remember to close your thread by marking the helpful post as answer, it is very beneficial to other community members who face the same issue.
Thanks for your understanding.
Best Regards,
Cherry
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Monday, November 18, 2019 6:25 PM
Thanks Cherry. The above code with some modification to suit my need has worked.