作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 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
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
最佳答案
您可以使用递归遍历每个目录和文件,如下所示:
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
ForEachFileAndFolder("yourPath", AddressOf dirAction, Addressof fileAction)
Public Sub dirAction(Byval dirInfo As DirectoryInfo)
' do something here '
End Sub
关于VB.NET 遍历文件和目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2266551/
我是一名优秀的程序员,十分优秀!