获取所有目录

时间:2014-10-13 22:36:50

标签: .net vb.net vb.net-2010

我按照此tutorial在Visual Basic 2010中创建了一个文件资源管理器。

这行代码Dim info As New DirectoryInfo("../..")获取程序启动的目录。

我想知道如何通过更改上一行代码强制程序列出C盘上的目录。

这是我的代码:

Private Sub PopulateTreeView()
    Dim rootNode As TreeNode

    Dim info As New DirectoryInfo("../..")
    If info.Exists Then
        rootNode = New TreeNode(info.Name)
        rootNode.Tag = info
        GetDirectories(info.GetDirectories(), rootNode)
        TreeView1.Nodes.Add(rootNode)
    End If

End Sub

Private Sub GetDirectories(ByVal subDirs() As DirectoryInfo, _
    ByVal nodeToAddTo As TreeNode)

    Dim aNode As TreeNode
    Dim subSubDirs() As DirectoryInfo
    Dim subDir As DirectoryInfo
    For Each subDir In subDirs
        aNode = New TreeNode(subDir.Name, 0, 0)
        aNode.Tag = subDir
        aNode.ImageKey = "folder"
        subSubDirs = subDir.GetDirectories()
        If subSubDirs.Length <> 0 Then
            GetDirectories(subSubDirs, aNode)
        End If
        nodeToAddTo.Nodes.Add(aNode)
    Next subDir

End Sub
Public Sub New()
    InitializeComponent()
    PopulateTreeView()

End Sub 'New
Private Sub treeView1_NodeMouseClick(ByVal sender As Object, _
ByVal e As TreeNodeMouseClickEventArgs) _
    Handles TreeView1.NodeMouseClick

    Dim newSelected As TreeNode = e.Node
    listView1.Items.Clear()
    Dim nodeDirInfo As DirectoryInfo = _
    CType(newSelected.Tag, DirectoryInfo)
    Dim subItems() As ListViewItem.ListViewSubItem
    Dim item As ListViewItem = Nothing

    Dim dir As DirectoryInfo
    For Each dir In nodeDirInfo.GetDirectories()
        item = New ListViewItem(dir.Name, 0)
        subItems = New ListViewItem.ListViewSubItem() _
            {New ListViewItem.ListViewSubItem(item, "Directory"), _
            New ListViewItem.ListViewSubItem(item, _
            dir.LastAccessTime.ToShortDateString())}

        item.SubItems.AddRange(subItems)
        listView1.Items.Add(item)
    Next dir
    Dim file As FileInfo
    For Each file In nodeDirInfo.GetFiles()
        item = New ListViewItem(file.Name, 1)
        subItems = New ListViewItem.ListViewSubItem() _
            {New ListViewItem.ListViewSubItem(item, "File"), _
            New ListViewItem.ListViewSubItem(item, _
            file.LastAccessTime.ToShortDateString())}

        item.SubItems.AddRange(subItems)
        listView1.Items.Add(item)
    Next file

    listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)

End Sub

3 个答案:

答案 0 :(得分:1)

你需要尝试捕捉,因为你将无权访问这些文件 因此,为了让代码跳过它,你可以使用Catch。

否则您会收到错误,例如您无法访问回收文件等等。

所以,当你开始为每个人试一试时。“就像下面的代码”

将Dim info更改为新目录信息(“../ ..”)到Dim info As New DirectoryInfo(“C:\”)

Private Sub GetDirectories(ByVal subDirs() As DirectoryInfo, _
ByVal nodeToAddTo As TreeNode)

    Dim aNode As TreeNode
    Dim subSubDirs() As DirectoryInfo
    Dim subDir As DirectoryInfo


    For Each subDir In subDirs

        Try

            aNode = New TreeNode(subDir.Name, 0, 0)
            aNode.Tag = subDir
            aNode.ImageKey = "folder"
            subSubDirs = subDir.GetDirectories()
            If subSubDirs.Length <> 0 Then
                GetDirectories(subSubDirs, aNode)
            End If
            nodeToAddTo.Nodes.Add(aNode)
        Catch ex As Exception

        End Try

    Next subDir

End Sub

答案 1 :(得分:1)

我明白了。我需要更改清单文件并以管理员身份运行程序。谢谢你的帮助。

答案 2 :(得分:0)

您的问题是,只有当您拥有对您选择枚举的路径的所有子目录的完全访问权限时,当前代码才有效。

至少您需要一些Try ... Catch ... End Try条款来报告和处理权限被拒绝的错误。