递归遍历目录

博客给出了一段递归遍历目录的代码。代码定义了 `getDirectory` 方法,通过 `Directory.GetFiles` 和 `Directory.GetDirectories` 获取文件和子目录,使用嵌套循环遍历,将文件完整路径添加到列表框,同时使用递归处理子目录,还包含异常处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  '****************************

'递归遍历目录

' 作者: 自由奔腾(wgscd)

' E-mail:wgscd@126.com

 Public Sub getDirectory(ByVal strpath As String)
        Try
            Dim file As FileInfo

            Dim str As String
            Dim strs As String() = Directory.GetFiles(strpath)
            Dim subdirectory As String
            Dim subdirectoryEntries As String() = Directory.GetDirectories(strpath)
            For Each subdirectory In subdirectoryEntries
                For Each str In strs
                    Me.Text = subdirectory
                    file = New FileInfo(str)

                    '  If file.FullName Like "*.jpg" Or file.FullName Like "*.bmp" Or file.FullName Like "*.gif" Or 'file.FullName Like "*.png" Then '可以添加指定的文件为你所用!!

                    ListBox1.Items.Add(file.FullName) '添加到列表里
                    Me.Text = file.FullName

                    ' End If

                Next str
                getDirectory(subdirectory) '递归
            Next subdirectory

        Catch ex As Exception

        End Try

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值