使用Combobox.Text中的目录填充Treeview

时间:2016-11-08 17:41:38

标签: vb.net combobox treeview

我有一个组合框,我可以从中选择文件夹名称。在我的D:\中搜索这个文件夹,当找到它们时,必须找到一个名为“Versions”的文件夹。然后,需要使用Treeview上的所有子文件夹填充此“Versions”子文件夹。关于如何做到这一点的任何想法,我真的碰到了这个?!?到目前为止我的代码(没有错误,但没有任何反应):

已编辑的代码(仍未正常工作):

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

这里是我需要的一个屏幕和另一个解释:

enter image description here

因此,第一个代码必须搜索名为“Microsoft”的文件夹,因为组合框项目显示。然后,在此文件夹中,必须对名为“Versions”的文件夹进行另一次搜索。最后,从“Versions”填充所有底层子文件夹/文件。没有与Treeview上的“版本”之前或同级别的文件夹!在这种情况下,我的“版本”路径是“D:\ MyDocuments \ Programs \ Microsoft \ Versions \” - 搜索路径不同,但都在“D:\”目录中,并且都包含“Versions”文件夹。

任何帮助非常感谢,提前感谢!!

1 个答案:

答案 0 :(得分:1)

这将填充Treeview文件夹和文件,因为它们在磁盘上组织...但是等等还有更多:

  

我需要显示的只是一个文件夹/文件名,

在重新编辑中:代码与前两个版本保持一致,只是为了“找到”起点和特定的子文件夹而打破了一些帮助:

Private Function FindVersionsFolder(startFolder As String) As String
    ' find a folder named "Versions" to be used as the start point
    ' note: can return "" when not found
    Dim curPath As String = Path.Combine(startFolder, "Versions")
    Dim temp As String = ""

    If Directory.Exists(curPath) Then
        Return curPath
    Else
        For Each d As String In Directory.EnumerateDirectories(startFolder)
            temp = FindVersionsFolder(d)
            If String.IsNullOrEmpty(temp) = False Then Return temp
        Next
    End If
    Return ""

End Function

Private Sub PopulateFolder(folder As String, parentNode As TreeNode,
                           Optional pattern As String = "*")
    ' create node for current folder, 
    '   add child folders
    '   add files contained
    Dim thisFolder As String
    Dim tn As TreeNode

    For Each d As String In Directory.EnumerateDirectories(folder, pattern)
        thisFolder = GetLastFolder(d)

        tn = New TreeNode(thisFolder, 1, 1)
        parentNode.Nodes.Add(tn)

        ' recurse to add child folders
        PopulateFolder(d, tn)
        ' populate files in this folder
        PopulateFiles(tn, d)
    Next
    ' if desired the files in base "VERSIONS" folder
    'PopulateFiles(tv1.Nodes(0), folder)
End Sub

Private Function GetLastFolder(fullPath As String) As String
    ' trim to the last folder segment
    Dim DirSep = Path.DirectorySeparatorChar
    Dim split = fullPath.Split(DirSep)
    Return split(split.Length - 1)
End Function

Private Sub PopulateFiles(tn As TreeNode, folder As String)
    ' add all files for a folder
    For Each f As String In Directory.EnumerateFiles(folder, "*.*")
        tn.Nodes.Add("", Path.GetFileName(f), 0)
    Next
End Sub

用法:

    Dim startFolder = "C:\Temp\Microsoft"
    ' modify root node
    tv1.TopNode.Text = GetLastFolder(startFolder)
    tv1.TopNode.ImageIndex = 1

    ' find the starting point
    startFolder = FindVersionsFolder(startFolder)

    ' populate TV from that point
    PopulateFolder(startFolder, tv1.Nodes(0), "Ver*")

它在文件夹之后添加起始文件夹的文件(如Explorer)。我不确定你为什么使用DirectoryInfo.GetFiles()) and getting a bunch of FileInfo objects if you just want the name. This uses Directory.EnumerateFiles()which is a little more efficient than GetFiles()`。

我的测试起点有一些障碍和文件夹/文件被排除在外:

enter image description here

结果似乎是你想要的。它还添加了一个图标,以便您可以告诉文件夹中的文件:

enter image description here enter image description here

要获取每种文件类型的关联图标,请参阅 Show folder icon in a listview 。不要让ListView部分抛出你 - 两个控件都使用ImageList作为图像。