VB.NET 迭代文件和目录
我正在开发一个 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
我正在尝试复制整个子目录的目录结构完好无损。我的第一次尝试将所有文件转储到一个目录中。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用递归来浏览每个目录和文件,如下所示:
编辑: 委托的用法:
与 FileAction 相同。
You could go through each directory and file using recursion like this:
Edit: Usage of the Delegates:
Same for the FileAction.
您必须分别处理目录和文件,但这仍然非常简单。
System.IO.DirectoryInfo 包含指定目录中的列表文件和子目录。查找属性目录和文件。
您需要做的是指定一个函数,该函数将首先读取 DirectoryInfo 中的所有文件,然后读取目录。对于文件,您可以对它执行您想要执行的操作(我认为是上传),一旦您点击目录,就使用子目录作为参数递归调用您的函数。这将遍历所有子目录和文件。
伪代码(我称其为伪代码,因为我不知道确切的语法)
希望有所帮助。
You'll have to handle directories and files separatly, but it's still pretty simple.
System.IO.DirectoryInfo constains the list files and sub-directories within a specified directory. Look for the property directories and files.
What you will want to do is specify a function that will read through all files first and then directories within the DirectoryInfo. For the files, you do what you want to do with it (Upload I think), and once you hit the Directories, call your function recursivly using the sub directory as the argument. This will go thourh all sub-directories and file.
Pseudo-Code (I call it pseudo-code cos' I don't know the exact syntax)
Hope that helps.
查看
DirectoryInfo
< /a> 类。Have a look at the
DirectoryInfo
class.