Vb.net 在treeview中显示特定文件夹/文件

Vb.net 在treeview中显示特定文件夹/文件,vb.net,winforms,treeview,Vb.net,Winforms,Treeview,在互联网上搜寻一种尽可能精简的方法之后,我没能找到适合我的东西。我从中获取了编码,试图使其工作,但无法找出如何拯救我的生命。以下是我所拥有的: 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.ObjectMo

在互联网上搜寻一种尽可能精简的方法之后,我没能找到适合我的东西。我从中获取了编码,试图使其工作,但无法找出如何拯救我的生命。以下是我所拥有的:

   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
Private Sub Form1\u Load(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
'获取驱动器列表
将驱动器设置为System.Collections.ObjectModel.ReadOnlyCollection(属于DriveInfo)=My.Computer.FileSystem.drives
Dim rootDir As String=String.empty
'现在循环通过每个驱动器并填充树视图
对于i,整数=0到驱动器。计数-1
rootDir=驱动器(i).名称
'将此驱动器添加为根节点
TreeView1.Nodes.Add(rootDir)
'填充此根节点
PopulateTreeView(rootDir,TreeView1.Nodes(i))
下一个
端接头
私有子PopulateTreeView(ByVal dir作为字符串,ByVal parentNode作为TreeNode)
Dim文件夹为String=String.Empty
尝试
Dim folders()为字符串=IO.Directory.GetDirectories(dir)
如果文件夹长度为0,则
Dim childNode作为TreeNode=无
对于文件夹中的每个文件夹
childNode=新树节点(文件夹)
parentNode.Nodes.Add(子节点)
PopulateTreeView(文件夹,子节点)
下一个
如果结束
捕获ex作为未经授权的访问例外
parentNode.Nodes.Add(文件夹&“:拒绝访问”)
结束尝试
端接头

但是,它只列出驱动器及其文件夹,我需要它从一开始就列出特定目录中的目录和文件,而不必一直沿着目录树查找您要查找的内容。我已尝试将
rootDir
更改为我试图访问的特定文件夹的地址,但无效。有人有什么建议吗?

我不确定我是否理解这个问题,但根据我在原始问题中的评论:这段代码将按照我在评论中所说的做。它是用C写的。另外:它可能还没有准备好生产,但这只是一个开始

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("");

    }
private void Form1\u加载(System.Object sender,System.EventArgs e)
{
//获取驱动器列表
DriveInfo[]drives=DriveInfo.GetDrives();
string rootDir=string.Empty;
//现在循环遍历每个驱动器并填充树视图

对于(int i=0;i您是否仅尝试在扩展的父级上显示文件夹/文件?您当前的实现将列出整个计算机上每个文件夹中的每个文件。这将花费很长时间。为什么不从顶级目录开始,当用户单击展开一个目录时,您就可以使用
PopulateTreeView
?速度更快,强度更低e、 我试图做的是使用上面的代码,它针对特定路径而不是所有驱动器工作。上面的代码已经在特定路径上工作。像这样调用PopulateTreeView:
PopulateTreeView(您的路径,TreeView1.Nodes(0))
。看看我下面的答案,你会注意到它是针对一个特定的路径执行的,并且在单击时被触发。谢谢Brandon!在我转换它之后,它可以满足我的需要,一点问题都没有。但是,有一件事我遇到了问题,这可能是很容易解决的,我只是太傻了,弄不明白,为什么它将只列出一个文件夹。例如:它列出驱动器,我展开其中一个驱动器,它列出文件夹/文件,我展开文件夹,什么都没有列出。有指针吗?所以这很奇怪。我在一个项目中提供了我在S.O.上帮助过人们的所有内容,因此我可以在以后的日期返回并参考它(出于这个确切的原因).我取消了您的问题注释(最初有效)突然,我遇到了您描述的问题…我想您复制并粘贴了我的代码?因为您使用的是C#,表单编辑器不会自动将事件处理代码连接到表单控件。请在设计器中单击树视图,在“属性”选项卡中选择闪电,然后更改“BeforeCollapse”和“BeforeExpand”还有:如果我的答案是被接受的答案,记得接受它作为你问题的答案;)还有两件事:它现在背靠背地列出了两个二级目录。我打开了顶层文件夹,所有内容都列出了两次。此外,我不想就此提出新问题。是否可以删除文件名中除实际文件名之外的所有内容。例如:C:\folder\index.html变成了just index.html?第二个两次列出的级别目录:检查树视图的事件。确保“BeforeClopse”设置为“Treeview1_BeforeClopse”,而“BeforeExpand”设置为“Treeview1_BeforeExpand”。通过将两个事件都设置为“Treeview1_BeforeExpand”,我可以复制双目录.我会贴张照片让事情变得更简单,但公司政策禁止imgur.com