Public Class FrmTest
'First solution
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Lst As Generic.List(Of System.IO.FileInfo)
Lst = GetFileRecursive("C:\temp", "3W5357*")
For Each f As System.IO.FileInfo In Lst
' your code
Next
End Sub
Private Function GetFileRecursive(ByVal Path As String, ByVal FileSearchPattern As String) As Generic.List(Of System.IO.FileInfo)
Dim d As New System.IO.DirectoryInfo(Path)
Return GetFileRecursive(d, FileSearchPattern)
End Function
Private Function GetFileRecursive(ByVal Path As System.IO.DirectoryInfo, ByVal FileSearchPattern As String) As Generic.List(Of System.IO.FileInfo)
Dim lstfile As New Generic.List(Of System.IO.FileInfo)
'first step
'Add file
For Each s As String In System.IO.Directory.GetFiles(Path.FullName, FileSearchPattern)
Dim F As New System.IO.FileInfo(s)
lstfile.Add(F)
Next
'Serch for subdirectory
For Each s As String In System.IO.Directory.GetDirectories(Path.FullName)
lstfile.AddRange(GetFileRecursive(s, FileSearchPattern))
Next
Return lstfile
End Function
'Second solution
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Lst As New MyFile("C:\temp", "3W5357*")
For Each f As System.IO.FileInfo In Lst
' your code
Next
End Sub
End Class
Public Class MyFile
Implements IEnumerable
Private MyPath As String
Private MyFileSearchPath As String
Public Sub New(ByVal Path As String, ByVal FileSearchPath As String)
MyBase.New()
MyPath = Path
MyFileSearchPath = FileSearchPath
End Sub
Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return New MyFileEnum(MyPath, MyFileSearchPath)
End Function
End Class
Public Class MyFileEnum
Implements IEnumerator
Private Position As Integer = -1
Private MyLst As Generic.List(Of System.IO.FileInfo)
Private Function GetFileRecursive(ByVal Path As String, ByVal FileSearchPattern As String) As Generic.List(Of System.IO.FileInfo)
Dim d As New System.IO.DirectoryInfo(Path)
Return GetFileRecursive(d, FileSearchPattern)
End Function
Private Function GetFileRecursive(ByVal Path As System.IO.DirectoryInfo, ByVal FileSearchPattern As String) As Generic.List(Of System.IO.FileInfo)
Dim lstfile As New Generic.List(Of System.IO.FileInfo)
'first step
'Add file
For Each s As String In System.IO.Directory.GetFiles(Path.FullName, FileSearchPattern)
Dim F As New System.IO.FileInfo(s)
lstfile.Add(F)
Next
'Serch for subdirectory
For Each s As String In System.IO.Directory.GetDirectories(Path.FullName)
lstfile.AddRange(GetFileRecursive(s, FileSearchPattern))
Next
Return lstfile
End Function
Public Sub New(ByVal Path As String, ByVal FileSearchPath As String)
MyBase.New()
MyLst = GetFileRecursive(Path, FileSearchPath)
End Sub
Public ReadOnly Property Current() As Object Implements System.Collections.IEnumerator.Current
Get
Try
Return MyLst(Position)
Catch ex As IndexOutOfRangeException
Throw New InvalidOperationException
End Try
End Get
End Property
Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
Position = Position + 1
Return (Position < MyLst.Count)
End Function
Public Sub Reset() Implements System.Collections.IEnumerator.Reset
Position = -1
End Sub
End Class