2016-11-08 108 views
0

我有一个组合框,我从中选择文件夹名称。在我的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 

这里是截图和我需要的另一种解释:

enter image description here

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

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

+0

这是一个调试问题。代码运行时,MyCombo.Text的价值是什么? – LarsTech

+0

@LarsTech,MyCombo.Text的值是“Microsoft” - 所以它应该搜索D:\中的Microsoft文件夹。搜索路径是O.K.正如我在调试器中看到的那样,它在“针对每个f作为FileInfo”中进行了调查。 – LuckyLuke82

+0

代码以其他方式发布。 – LarsTech

回答

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*") 

它增加了文件夹(如资源管理器)后启动文件夹中。我不知道你为什么使用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 hereenter image description here


要获得每个文件类型关联的图标,看到Show folder icon in a listview。不要让ListView部件引发你 - 两个控件都使用ImageList来处理图像。

+0

谢谢,我会测试这个明天。尽管我需要显示的只是一个文件夹/文件名,但不包括您在示例中提供的路径。我仍然需要部分代码来搜索我的“微软”文件夹...但是我想我可以改变它......看起来很好用的图标 - 这里有个问题 - 我可以使用像Explorer这样的文件的图标吗? (从.dll如果我没有错) – LuckyLuke82

+0

我测试了你的代码,但我不适合我,我得到错误“指定的参数超出了有效值的范围。”在“tv1.Nodes(0).Text = folder”行中。看看我编辑的问题,你会看到我提供的图像下的另一种解释。 – LuckyLuke82