VB.NET iterating through files and directories


I'm working on a VB.NET program that will automatically backup my work to my FTP server. So far I am able to upload a single file, by specifying a file name using this:

     'relevant part - above is where FTP object is instantiated
       Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()

                'Stream to which the file to be upload is written
                Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()

                'Read from the file stream 2kb at a time
                Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)

                'Till Stream content ends
                Do While contentLen <> 0
                    ' Write Content from the file stream to the FTP Upload Stream
                    _Stream.Write(buff, 0, contentLen)
                    contentLen = _FileStream.Read(buff, 0, buffLength)

                ' Close the file stream and the Request Stream

            Catch ex As Exception
                MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

Now I want to be able to iterate through a directory (Which contains subdirectories) and recursively call the function above. I'm having a problem getting a hold of the files in the directory.

My Question is, How can I loop through and send each file to the upload function above? Can I just send the filename to an array, or is there some sort of system object to handle this (e.g. System.IO.Directory)

Pseudocode for what I'm trying to do

                For Each Sub_directory In Source_directory

                     For Each File in Directory
                       'call the above code to transfer the file

                'start next subdirectory

I'm trying to replicate the entire directory structure with sub directories intact. My first attempt dumped ALL files into one directory.

Best Solution

You could go through each directory and file using recursion like this:

Public Shared Sub ForEachFileAndFolder(ByVal sourceFolder As String, _
                                       ByVal directoryCallBack As Action(Of DirectoryInfo), _
                                       ByVal fileCallBack As Action(Of FileInfo))

    If Directory.Exists(sourceFolder) Then
            For Each foldername As String In Directory.GetDirectories(sourceFolder)
                If directoryCallBack IsNot Nothing Then
                    directoryCallBack.Invoke(New DirectoryInfo(foldername))
                End If

                ForEachFileAndFolder(foldername, directoryCallBack, fileCallBack)
        Catch ex As UnauthorizedAccessException
        End Try

        If fileCallBack IsNot Nothing
            For Each filename As String In Directory.GetFiles(sourceFolder)
                fileCallBack.Invoke(New FileInfo(filename))
        End If
    End If
End Sub

Edit: Usage of the Delegates:

ForEachFileAndFolder("yourPath", AddressOf dirAction, Addressof fileAction)

Public Sub dirAction(Byval dirInfo As DirectoryInfo)
    ' do something here '
End Sub

Same for the FileAction.