I am working on a VB.NET program that will automatically back up my work to my FTP server. For now, I can upload one 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 call the function recursively above. I have a problem storing files in a directory.
My question is: how can I execute a loop and send each file to the upload function above? Can I just send the file name to an array or is there some kind 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 am trying to replicate the entire directory structure with unprepared subdirectories. My first attempt dumped ALL files into a single directory.
source share