在treeview中显示SPECIFIC文件夹/文件

时间:2014-01-28 16:26:59

标签: vb.net winforms treeview

在搜索互联网以寻找尽可能精简的方法之后,我无法找到适合我的东西。我已经从this question获取了编码,试图让它发挥作用但却无法弄清楚如何挽救我的生命。这就是我所拥有的:

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Get a list of drives
    Dim drives As System.Collections.ObjectModel.ReadOnlyCollection(Of DriveInfo) = My.Computer.FileSystem.Drives
    Dim rootDir As String = String.empty
    'Now loop thru each drive and populate the treeview
    For i As Integer = 0 To drives.Count - 1
        rootDir = drives(i).Name
        'Add this drive as a root node
        TreeView1.Nodes.Add(rootDir)
        'Populate this root node
        PopulateTreeView(rootDir, TreeView1.Nodes(i))
    Next

End Sub

Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)
    Dim folder As String = String.Empty
    Try
        Dim folders() As String = IO.Directory.GetDirectories(dir)
        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
    Catch ex As UnauthorizedAccessException
        parentNode.Nodes.Add(folder & ": Access Denied")
    End Try
End Sub

然而,它只列出了驱动器和驱动器。他们的文件夹,我需要它从一开始就列出特定目录中的目录和文件,而不必一直到树下找到你特别想要的东西。我尝试将rootDir更改为我尝试访问的特定文件夹的地址,但无济于事。有没有人有任何建议?

1 个答案:

答案 0 :(得分:2)

我不确定我是否理解这个问题,但根据我在原始问题中的评论:此代码将执行我在该评论中所说的内容。它是用C#but here is a converter编写的。另外:它可能不是生产准备,但它是一个开始。

private void Form1_Load(System.Object sender, System.EventArgs e)
    {
        //Get a list of drives
        DriveInfo[] drives = DriveInfo.GetDrives();
        string rootDir = string.Empty;
        //Now loop thru each drive and populate the treeview
        for (int i = 0; i <= drives.Length - 1; i++)
        {
            rootDir = drives[i].Name;
            //Add this drive as a root node
            TreeView1.Nodes.Add(rootDir);
            //Populate this root node
            PopulateTreeView(rootDir, TreeView1.Nodes[i]);
        }

    }
    private void PopulateTreeView(string dir, TreeNode parentNode)
    {
        string folder = string.Empty;
        try
        {
            string[] folders = System.IO.Directory.GetDirectories(dir);
            if (folders.Length != 0)
            {
                TreeNode childNode = null;
                foreach (string folder_loopVariable in folders)
                {
                    folder = folder_loopVariable;
                    childNode = new TreeNode(folder);
                    childNode.Nodes.Add("");
                    parentNode.Nodes.Add(childNode);
                }
            }
            string[] files = System.IO.Directory.GetFiles(dir);
            if (files.Length != 0)
            {
                TreeNode childNode = null;
                foreach (string file in files)
                {
                    childNode = new TreeNode(file);
                    parentNode.Nodes.Add(childNode);
                }
            }
        }
        catch (UnauthorizedAccessException ex)
        {
            parentNode.Nodes.Add(folder + ": Access Denied");
        }
    }

    private void TreeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
    {
        TreeNode actual = e.Node;
        actual.Nodes[0].Remove();
        PopulateTreeView(actual.Text, actual);
    }

    private void TreeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
    {
        TreeNode actual = e.Node;
        actual.Nodes.Clear();
        actual.Nodes.Add("");

    }