我有一个组合框,我从中选择文件夹名称。在我的D:\中搜索这个文件夹,并且当它们被找到时,在它中找到一个名为“Versions”的文件夹也必须找到。然后,这个“版本”子文件夹需要在树状视图中填充所有子文件夹。关于如何做到这一点的任何想法,我真的碰到了这个?!?到目前为止我的代码(没有错误,但没有任何反应):从Combobox.Text填充Treeview目录
编辑的代码(仍然没有工作):
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim folder1 As String() = Directory.GetDirectories("D:\", MyCombo.Text, System.IO.SearchOption.AllDirectories)
For Each folder1 As String In MyDirectory
Dim SubDirectories As String() = IO.Directory.GetDirectories(folder1, "*Versions*", System.IO.SearchOption.AllDirectories)
For Each subfolder In SubDirectories
PopulateFolder(subfolder)
Next
Next
End Sub
Private Sub PopulateFolder(folder As String)
tv1.Nodes(0).Text = folder
tv1.Nodes(0).ImageIndex = 1
Dim DirSep = Path.DirectorySeparatorChar
Dim thisFolder As String
Dim tn As TreeNode
For Each d As String In Directory.EnumerateDirectories(folder)
' split the path to get the last segment
Dim split = d.Split(DirSep)
thisFolder = split(split.Length - 1)
tn = New TreeNode(thisFolder, 1, 1)
tv1.Nodes(0).Nodes.Add(tn)
PopulateFiles(tn, d)
Next
PopulateFiles(tv1.Nodes(0), folder)
End Sub
Private Sub PopulateFiles(tn As TreeNode, folder As String)
For Each f As String In Directory.EnumerateFiles(folder, "*.*")
' Path will extract the name:
tn.Nodes.Add("", Path.GetFileName(f), 0)
Next
End Sub
这里是截图和我需要的另一种解释:
因此,第一个代码必须搜索名为“Microsoft”的文件夹作为组合框项目显示。然后,在该文件夹中,对于名为“版本”的文件夹,必须进行另一次搜索。最后,从“版本”填充所有基础子文件夹/文件。在Treeview的“版本”之前或之后的文件夹中没有文件夹!在这种情况下,我的“版本”路径是“D:\ MyDocuments \ Programs \ Microsoft \ Versions \” - 搜索路径不同,但都位于“D:\”目录中,全部包含“版本”文件夹。
任何帮助非常感谢,提前致谢!
这是一个调试问题。代码运行时,MyCombo.Text的价值是什么? – LarsTech
@LarsTech,MyCombo.Text的值是“Microsoft” - 所以它应该搜索D:\中的Microsoft文件夹。搜索路径是O.K.正如我在调试器中看到的那样,它在“针对每个f作为FileInfo”中进行了调查。 – LuckyLuke82
代码以其他方式发布。 – LarsTech