您可以使用以下代码来实现通过TreeView控件显示文件夹下所有文件列表的功能:
Private Sub PopulateTreeView(ByVal path As String, ByVal parentNode As TreeNode)
Dim folder As String = String.Empty
Try
Dim folders() As String = IO.Directory.GetDirectories(path)
If folders.Length <> 0 Then
Dim childNode As TreeNode = Nothing
For Each folder In folders
childNode = New TreeNode(folder)
parentNode.Nodes.Add(childNode)
PopulateTreeView(folder, childNode)
Next
End If
Dim files() As String = IO.Directory.GetFiles(path)
If files.Length <> 0 Then
Dim childNode As TreeNode = Nothing
For Each file In files
childNode = New TreeNode(IO.Path.GetFileName(file))
parentNode.Nodes.Add(childNode)
Next
End If
Catch ex As UnauthorizedAccessException
parentNode.Nodes.Add("Access Denied")
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rootPath As String = "C:\YourFolderPath" '指定文件夹路径
Dim rootNode As New TreeNode(rootPath)
TreeView1.Nodes.Add(rootNode)
PopulateTreeView(rootPath, rootNode)
End Sub
这段代码通过递归遍历文件夹,将每个文件夹和文件都添加到TreeView控件中的对应节点。您只需要将rootPath
变量替换为您想显示文件列表的文件夹路径即可。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: vb.net连接数据库的方法是什么