Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用文件名筛选器查找文件_Vb.net_List_Search_Visual Studio 2012_Filtering - Fatal编程技术网

Vb.net 使用文件名筛选器查找文件

Vb.net 使用文件名筛选器查找文件,vb.net,list,search,visual-studio-2012,filtering,Vb.net,List,Search,Visual Studio 2012,Filtering,我正在使用VB.net VS2012,在获取带有过滤器的文件列表时遇到问题 这是我的密码: Public Function SearchAndAddToListWithFilter(ByVal path As String, ByVal Recursive As Boolean, arrayListOfFilters As ArrayList, ByRef listOfFiles As List(Of FileInfo)) If Not Directory.Exists(path) Th

我正在使用VB.net VS2012,在获取带有过滤器的文件列表时遇到问题

这是我的密码:

Public Function SearchAndAddToListWithFilter(ByVal path As String, ByVal Recursive As Boolean, arrayListOfFilters As ArrayList, ByRef listOfFiles As List(Of FileInfo))
    If Not Directory.Exists(path) Then Exit Function

    Dim initDirInfo As New DirectoryInfo(path)

    For Each oFileInfo In initDirInfo.GetFiles
        Application.DoEvents()
        For x = 0 To arrayListOfFilters.Count - 1
            If (oFileInfo.Name Like arrayListOfFilters(x)) Then
                listOfFiles.Add(oFileInfo)
            End If
        Next
    Next

    If Recursive Then
        For Each oDirInfo In initDirInfo.GetDirectories
            SearchAndAddToListWithFilter(oDirInfo.FullName, True, arrayListOfFilters, listOfFiles)
        Next
    End If

End Function
下面是一个如何使用它的示例:

    Dim stringFilterList As String = "*.mp3, *.docx, *.mp3, *.txt"
    Dim arrayListOfFilenameFilters As New ArrayList(stringFilterList.Split(","))
    Dim stringFolderPath As String = "C:\temp\folder\"
    Dim booleanSearchSubFolders As Boolean = True

    Dim listOfFilesFoundViaSearch As New List(Of FileInfo)
    SearchAndAddToListWithFilter(stringFolderPath, booleanSearchSubFolders, arrayListOfFilenameFilters, listOfFilesFoundViaSearch)

    For x = 0 To listOfFilesFoundViaSearch.Count - 1
        MsgBox(listOfFilesFoundViaSearch(x).FullName)
    Next
出于某种原因,代码只将满足过滤器列表中第一个条件的文件添加到列表中

我能不能请你帮个忙让这个代码正常工作


谢谢。

函数返回值,而通过ByRef传递值并不是实现这一点的方法

以下功能将起作用:

Private Function SearchAndAddToListWithFilter(ByVal path As String, ByVal filters As String(), ByVal searchSubFolders As Boolean) As List(Of IO.FileInfo)
    If Not IO.Directory.Exists(path) Then
        Throw New Exception("Path not found")
    End If

    Dim searchOptions As IO.SearchOption
    If searchSubFolders Then
        searchOptions = IO.SearchOption.AllDirectories
    Else
        searchOptions = IO.SearchOption.TopDirectoryOnly
    End If

    Return filters.SelectMany(Function(filter) New IO.DirectoryInfo(path).GetFiles(filter, searchOptions)).ToList
End Function
要使用此功能,请执行以下操作:

Dim filters As String() = {"*.mp3", "*.docx", "*.bmp", "*.txt"}
Dim path As String = "C:\temp\folder\"

Dim foundFiles As List(Of IO.FileInfo) = SearchAndAddToListWithFilter(path, filters, True)

@Steve提供的解决方案真正展示了.NET完成任务的方式。 然而,我使用了一个递归解决方案,其中可能定义了最大深度和/或持续时间。为了完成本主题,我想发布以下代码:

“”
''在目录和子目录中搜索文件
''' 
''开始目录
''搜索模式
“最大深度”;0表示无限深度
“最长持续时间”;0无限期
''包括路径的文件名列表
''' 
“子目录的递归使用”
''' 
“沃纳-novak@bemessung.at
''' 
递归地使用公共共享函数dir(searchDir作为字符串,searchPattern作为字符串_
可选的maxDepth作为整数=0_
可选maxDurationMS(长度=0)作为列表(字符串)
将文件列表变暗为新列表(字符串)
将深度调整为整数=0
将sw调暗为新秒表
目录(searchDir、searchPattern、maxDepth、maxDurationMS、文件列表、深度、sw)
返回文件列表
端函数
''' 
''递归文件搜索
''' 
''开始目录
''搜索模式
“最大深度”;0表示无限深度
“最长持续时间”;0无限期
要附加到的“”文件列表
“当前深度”
“秒表”
''要提前退出的布尔值(在给定的深度或持续时间)
''' 
“沃纳-novak@bemessung.at
''' 
私有共享子目录(searchDir作为字符串,searchPattern作为字符串_
可选的maxDepth作为整数=0_
可选maxDurationMS,长度为0_
可选ByRef文件列表作为列表(字符串)=无_
可选ByRef深度为整数=0_
可选ByRef sw作为秒表=无_
可选ByRef退出为布尔值=False)
如果maxDurationMS>0,则
如果深度=0,则
sw=新秒表
sw.Start()
其他的
如果sw.ElapsedMilliseconds>maxDurationMS,则
退出=真
出口接头
如果结束
如果结束
如果结束
如果maxDepth>0,则
如果深度>最大深度,则
退出=真
出口接头
如果结束
如果结束
'检查目录是否存在
如果目录不存在(searchDir),则
出口接头
如果结束
'查找文件
对于Directory.GetFiles(searchDir、searchPattern)中的每个myFile作为字符串
fileList.Add(myFile)
下一个
'递归扫描子目录
对于Directory.GetDirectories(searchDir)中的每个myDir
深度+=1
目录(myDir、searchPattern、maxDepth、maxDurationMS、文件列表、深度、sw、退出)
如果退出,则退出
深度-=1
下一个
端接头
列表视图1.Items.Clear()


试试这个最简单的方法。购买方式不介意imagelist仅适用于文件图标。首先,您需要从cmb_Drives获取逻辑驱动器。选择EditEMS.Tostring()和文本框中文件的起始字母,从ListviewSubItems中填充文件。您可以编辑帖子,而不必留下评论。欢迎来到stackoverflow!
''' <summary>
''' Search files in directory and subdirectories 
''' </summary>
''' <param name="searchDir">Start Directory</param>
''' <param name="searchPattern">Search Pattern</param>
''' <param name="maxDepth">maximum depth; 0 for unlimited depth</param>
''' <param name="maxDurationMS">maximum duration; 0 for unlimited duration</param>
''' <returns>a list of filenames including the path</returns>
''' <remarks>
''' recursive use of   Sub dirS
''' 
''' wallner-novak@bemessung.at
''' </remarks>
Public Shared Function dirRecursively(searchDir As String, searchPattern As String, _
                                    Optional maxDepth As Integer = 0, _
                                    Optional maxDurationMS As Long = 0) As List(Of String)
    Dim fileList As New List(Of String)
    Dim depth As Integer = 0
    Dim sw As New Stopwatch

    dirS(searchDir, searchPattern, maxDepth, maxDurationMS, fileList, depth, sw)

    Return fileList

End Function

''' <summary>
''' Recursive file search
''' </summary>
''' <param name="searchDir">Start Directory</param>
''' <param name="searchPattern">Search Pattern</param>
''' <param name="maxDepth">maximum depth; 0 for unlimited depth</param>
''' <param name="maxDurationMS">maximum duration; 0 for unlimited duration</param>
''' <param name="fileList">Filelist to append to</param>
''' <param name="depth">current depth</param>
''' <param name="sw">stopwatch</param>
''' <param name="quit">boolean value to quit early (at given depth or duration)</param>
''' <remarks>
''' wallner-novak@bemessung.at
''' </remarks>
Private Shared Sub dirS(searchDir As String, searchPattern As String, _
                            Optional maxDepth As Integer = 0, _
                            Optional maxDurationMS As Long = 0, _
                            Optional ByRef fileList As List(Of String) = Nothing, _
                            Optional ByRef depth As Integer = 0, _
                            Optional ByRef sw As Stopwatch = Nothing, _
                            Optional ByRef quit As Boolean = False)

    If maxDurationMS > 0 Then
        If depth = 0 Then
            sw = New Stopwatch
            sw.Start()
        Else
            If sw.ElapsedMilliseconds > maxDurationMS Then
                quit = True
                Exit Sub
            End If
        End If
    End If

    If maxDepth > 0 Then
        If depth > maxDepth Then
            quit = True
            Exit Sub
        End If
    End If

    ' check if directory exists
    If Not Directory.Exists(searchDir) Then
        Exit Sub
    End If

    ' find files
    For Each myFile As String In Directory.GetFiles(searchDir, searchPattern)
        fileList.Add(myFile)
    Next

    ' recursively scan subdirectories 
    For Each myDir In Directory.GetDirectories(searchDir)
        depth += 1
        dirS(myDir, searchPattern, maxDepth, maxDurationMS, fileList, depth, sw, quit)
        If quit Then Exit For
        depth -= 1
    Next

End Sub
        For Each files As String In System.IO.Directory.GetFiles(cmb_Drives.SelectedItem.ToString, txtSearch.Text)

            Dim ico As Icon = System.Drawing.Icon.ExtractAssociatedIcon(files)
            ImageList1.Images.Add(ico)

            Dim list As ListViewItem = New ListViewItem(My.Computer.FileSystem.GetFileInfo(files).FullName, ImageList1.Images.Count - 1)
            ListView1.Items.Add(list)

        Next