Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am well aware of how to call a function inside a button when using a label or text box to say change text or to do small tasks of that matter. My problem is trying to call a function to carry out my enumeration function?
VB
Private Iterator Function GetFilesRecursive(ByVal dirInfo As DirectoryInfo, ByVal searchPattern As String) As IEnumerable(Of FileInfo)
        searchPattern = ComboBox1.SelectedItem
        For Each di As DirectoryInfo In dirInfo.GetDirectories()
            For Each fi As FileInfo In GetFilesRecursive(di, searchPattern)
               
                Yield fi
            Next fi
        Next di

        For Each fi As FileInfo In dirInfo.GetFiles(searchPattern)
            
            Yield fi
        Next fi
    End Function

thank you in advance!!
Posted
Updated 9-Jan-12 20:58pm
v2

1 solution

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
 
Share this answer
 
v2
Comments
Dale 2012 14-Jan-12 11:02am    
OMG thank you so much !! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900