2012-11-25 45 views
0

我正在使用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 

出於某種原因,代碼只是將文件添加到satisy的列表過濾器列表中的第一個條件。

我可以請一些幫助讓這段代碼工作嗎?

謝謝。

回答

1

函數返回值,並傳遞一個值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) 
1

由@Steve提供的解決方案真正體現在做任務的.NET方式。 但是我使用了一個遞歸解決方案,可能定義了最大深度和/或持續時間。有關此主題的完整性,我要發佈代碼:

''' <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 
''' 
''' [email protected] 
''' </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> 
''' [email protected] 
''' </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 
0

ListView1.Items.Clear()

 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 
+0

嘗試這種最簡單的方法。購買方式不介意imagelist僅適用於文件圖標。首先需要做的是,從cmb_drives.selecteditems.Tostring()獲取邏輯驅動器,並從文本框中獲取文件的開始字母以填充ListviewSubItems中的文件。 –

+0

你可以編輯你的文章,而不是留下評論。歡迎來到stackoverflow! – Clay