获取VB.net中的所有文件夹/目录列表 [英] Get all folder / directories list in VB.net

查看:131
本文介绍了获取VB.net中的所有文件夹/目录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个 Stackoverflow 问题,我正在学习 VB,但在获取系统上所有文件夹/目录的列表时遇到了一些问题.我正在使用此处包含的代码,它似乎可以正常工作,直到它到达回收站文件夹和其他一些系统文件夹

This is my first Stackoverflow question, I'm learning VB and having a few problems with getting a list of all folders/directories on the system. I'm using the code included here and it seems to work until it hits the recycle bin folder, and some other system folders

Sub main()

    Dim DirList As New ArrayList
    GetDirectories("c:\", DirList)


    For Each item In DirList
        'add item to listbox or text etc here
    Next

End Sub

Sub GetDirectories(ByVal StartPath As String, ByRef DirectoryList As ArrayList)
    Dim Dirs() As String = Directory.GetDirectories(StartPath)
    DirectoryList.AddRange(Dirs)

    For Each Dir As String In Dirs
        GetDirectories(Dir, DirectoryList)
    Next
End Sub

谁能帮我解决这个问题?我想首先知道是什么导致了这种情况,以及一个好的解决方法或替代方法.

Can anyone help me with this? I'd like to know what is causing this first, and a good fix, or alternative way to do this.

提前致谢.

推荐答案

不允许访问某些文件夹.您可以在 Directory.GetDirectories(StartPath) 周围使用 Try-Catch 块,也可以事先检查文件夹的属性.

Access to some folders is not allowed. You can use a Try-Catch block around the Directory.GetDirectories(StartPath), or you can check the properties of the folder beforehand.

这篇关于获取VB.net中的所有文件夹/目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆