【问题标题】:Fast file listing incl sub folders快速文件列表,包括子文件夹
【发布时间】:2012-02-24 08:19:02
【问题描述】:

检索目录(包括子文件夹)中所有文件的最快方法是什么。目前我正在使用这个功能:

    Public Function FindFiles(path As String, Recursive As Boolean) As Boolean
    Dim dirInfo As New IO.DirectoryInfo(path)
    Dim fileObject As FileSystemInfo

    If Recursive = True Then
        For Each fileObject In dirInfo.GetFileSystemInfos()
            If System.IO.Directory.Exists(fileObject.FullName) Then
                FindFiles(fileObject.FullName, Recursive)
            Else
                'fileObject.FullName - found file
            End If
        Next
    Else
        For Each fileObject In dirInfo.GetFileSystemInfos()
            If Not System.IO.Directory.Exists(fileObject.FullName) Then
                'fileObject.FullName - found file
            End If
        Next
    End If

End Function

谢谢

【问题讨论】:

  • Recursive参数有什么用?我认为它可以被删除。

标签: vb.net


【解决方案1】:

试试这个

Private Function getAllFolders(ByVal directory As String) As String()
    Dim fi As New IO.DirectoryInfo(directory)         'Create object
    Dim path() As String = {}                         'Array to store paths
    'Loop through subfolders
    For Each subfolder As IO.DirectoryInfo In fi.GetDirectories()
        Array.Resize(path, path.Length + 1)           'Add this folders name
        path(path.Length - 1) = subfolder.FullName
        'Recall function with each subdirectory
        For Each s As String In getAllFolders(subfolder.FullName)
            Array.Resize(path, path.Length + 1)
            path(path.Length - 1) = s
        Next
    Next
    Return path
End Function

【讨论】:

  • johntotetwoo:那只列出文件夹。我在寻找文件。
猜你喜欢
  • 2010-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多