vb.net 在树视图中显示所有驱动器、文件和子文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18090860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:34:40  来源:igfitidea点击:

Display all drives, files, and subfolders in treeview

vb.net

提问by user2658544

I'm looking for the simplest way to display all drives, files, and subfolders in a treeview. If someone's got a snippet of code to do this that they don't mind sharing I would really appreciate it.

我正在寻找在树视图中显示所有驱动器、文件和子文件夹的最简单方法。如果有人有一段代码可以做到这一点,并且他们不介意分享,我会非常感激。

The closest I've gotten was this code I tried using, but it gave me a "IOException was unhandled" error saying "The device is not ready." error at runtime (after about 5-10 sec) on the line below

我得到的最接近的是我尝试使用的这段代码,但它给了我一个“IOException 未处理”错误,说“设备未准备好”。运行时错误(大约 5-10 秒后)在下面的行

Dim folders() As String = IO.Directory.GetDirectories(dir)

underneath is the rest of the code

下面是其余的代码

Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim drives As System.Collections.ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) = My.Computer.FileSystem.Drives
    Dim rootDir As String = String.Empty
    For i As Integer = 0 To drives.Count - 1
        rootDir = drives(i).Name
        TreeView1.Nodes.Add(rootDir)
        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

回答by Steven Liekens

Seems like you're off to a good start. The IOExceptionyou receive is most likely caused by your procedure trying to list contents on an empty disc drive, which is obviously impossible.

看起来你有一个好的开始。在IOException您收到很可能是你的程序试图列表内容为空光盘驱动器,这显然是不可能造成。

The fix is simple:

修复很简单:

For i As Integer = 0 To drives.Count - 1
    If Not drives(i).IsReady Then
        Continue For
    End If
    rootDir = drives(i).Name
    TreeView1.Nodes.Add(rootDir)
    PopulateTreeView(rootDir, TreeView1.Nodes(i))
Next

Besides that, I recommend not loading folder contents until a node is clicked. Limit the recursive call to 1 level (current directory + content of all its subdirectories). That way, you get the best performance while still being able to determine whether a subdirectory should have the treeview expand button.

除此之外,我建议在单击节点之前不要加载文件夹内容。将递归调用限制为 1 级(当前目录 + 其所有子目录的内容)。这样,您可以获得最佳性能,同时仍然能够确定子目录是否应具有树视图展开按钮。