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() Try '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) Loop _Stream.Close() _Stream.Dispose() _FileStream.Close() _FileStream.Dispose() ' 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 Next 'start next subdirectory Next
I'm trying to replicate the entire directory structure with sub directories intact. My first attempt dumped ALL files into one directory.