VB.NET 遍历文件和目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2266551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET iterating through files and directories
提问by tpow
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:
我正在开发一个 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
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)
我的问题是,我如何循环并将每个文件发送到上面的上传功能?我可以将文件名发送到数组,还是有某种系统对象来处理这个问题(例如 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.
我正在尝试使用完整的子目录复制整个目录结构。我的第一次尝试将所有文件都转储到一个目录中。
回答by Bobby
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
Try
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)
Next
Catch ex As UnauthorizedAccessException
Trace.TraceWarning(ex.Message)
End Try
If fileCallBack IsNot Nothing
For Each filename As String In Directory.GetFiles(sourceFolder)
fileCallBack.Invoke(New FileInfo(filename))
Next
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.
FileAction 也是如此。
回答by David Brunelle
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.
System.IO.DirectoryInfo 包含指定目录中的列表文件和子目录。查找属性目录和文件。
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.
您需要做的是指定一个函数,该函数将首先读取所有文件,然后读取 DirectoryInfo 中的目录。对于文件,您可以使用它做您想做的事情(我认为是上传),一旦您点击目录,就可以使用子目录作为参数递归调用您的函数。这将转到所有子目录和文件。
Pseudo-Code (I call it pseudo-code cos' I don't know the exact syntax)
伪代码(我称之为伪代码因为我不知道确切的语法)
Sub UploadDirectory(oDirInfo as DirectoryInfo) For eaco oFile as FileInfo in oDirINfo.Files 'Do your thing Next For each oDir as DirectoryInfo as oDirInfo.Directories ' Do some stuff if you need to separate directories UploadDirectory(odir) End sub
Hope that helps.
希望有帮助。
回答by Klaus Byskov Pedersen
Have a look at the DirectoryInfo
class.
看看DirectoryInfo
班级。