我正在开发一个VB.NET程序,它可以自动将我的工作备份到FTP服务器上。目前,我能够通过使用以下方法指定文件名来上传单个文件:
'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
现在我希望能够遍历一个目录(其中包含子目录),并递归调用上面的函数。我在获取目录中的文件方面遇到了问题。
我的问题是,如何循环遍历并将每个文件发送到上面的上传函数?我可以将文件名发送到数组中,还是有一些系统对象来处理这个问题(例如System.IO.Directory)?
我正在尝试做的伪代码:
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
我正在尝试复制整个目录结构,包括子目录。我的第一次尝试将所有文件都倒入一个目录中。