vb.net 搜索子目录下的文件

For Each filename As String In IO.Directory.GetFiles("c:\windows", "*.csv")
Dim file As IO.FileInfo = New IO.FileInfo(filename)
MsgBox(file.FullName)
Next

上面的代码只能 搜索到 当前文件夹的 .csv, 但我想要的是 当前文件夹以及文件夹下面的子文件夹里的 csv 有办法么?
已经自己 解决了。

For Each filename As String In IO.Directory.GetFiles("C:\Users\qc\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\", "*.csv",IO.SearchOption.AllDirectories)
Dim file As IO.FileInfo = New IO.FileInfo(filename)
MsgBox(file.FullName)
Next

vb.net编程查找搜索指定目录下面的所有文件和其子目录下的文件,方法如下:

''=============================================
''名称: FindPath
''作用: æŸ¥æ‰¾æœç´¢æŒ‡å®šç›®å½•ä¸‹é¢çš„所有文件和其子目录下的文件
''参数:strPath è¦æŸ¥æ‰¾çš„目录,
''strFiles ç”¨äºŽå­˜æŸ¥æ‰¾ç»“果的缓冲区,String ç±»åž‹çš„动态数组,调用时事先初始化, å¦‚Redim strFiles(0)
''FileCount ç”¨äºŽè¿”回文件个数
''=============================================
Public Sub FindPath(ByVal strPath As String, strFiles() As String, FileCount As Long)
Dim strDirs()   As String
Dim strResult   As String
Dim FileLimit   As Long
Dim dirLimit    As Long
Dim dirCount    As Long
Dim I           As Long
    
    FileLimit = UBound(strFiles) + 1
    dirLimit = 0
    If Right$(strPath, 1) <> "/" Then strPath = strPath & "/"
    strResult = Dir(strPath, vbDirectory + vbSystem + vbReadOnly + vbHidden + vbNormal + vbArchive)
    Do While Len(strResult) > 0
        If strResult <> "." And strResult <> ".." Then
            If (GetAttr(strPath & strResult) And vbDirectory) <> vbDirectory Then
                If FileCount >= FileLimit Then
                    ReDim Preserve strFiles(FileLimit + 10)
                    FileLimit = FileLimit + 10
                End If
                strFiles(FileCount) = strPath & strResult
                FileCount = FileCount + 1
            Else
                If dirCount >= dirLimit Then
                    ReDim Preserve strDirs(dirLimit + 10)
                    dirLimit = dirLimit + 10
                End If
                strDirs(dirCount) = strPath & strResult
                dirCount = dirCount + 1
            End If
        End If
        strResult = Dir(, vbDirectory + vbSystem + vbReadOnly + vbHidden + vbNormal + vbArchive)
    Loop
    
    For I = 0 To dirCount - 1
        Call FindPath(strDirs(I), strFiles, FileCount)
    Next I
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-03-22
For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\windows", FileIO.SearchOption.SearchAllSubDirectories, "*.csv")
            ListBox1.Items.Add(foundFile)
        Next

我用了列表框显示,你自己改一下

本回答被提问者和网友采纳
相似回答