加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

VB.NET迭代文件和目录

发布时间:2020-12-17 07:24:35 所属栏目:百科 来源:网络整理
导读:我正在研究一个VB.NET程序,它会自动将我的工作备份到我的FTP服务器上.到目前为止,我可以通过使用以下方法指定文件名来上传单个文件: 'relevant part - above is where FTP object is instantiated Dim _FileStream As System.IO.FileStream = _FileInfo.Ope
我正在研究一个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,buffLength)

                'Till Stream content ends
                Do While contentLen <> 0
                    ' Write Content from the file stream to the FTP Upload Stream
                    _Stream.Write(buff,contentLen)
                    contentLen = _FileStream.Read(buff,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

我正在尝试复制整个目录结构,子目录完好无损.我的第一次尝试将所有文??件转储到一个目录中.

解决方法

您可以使用递归遍历每个目录和文件,如下所示:

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

FileAction也是如此.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读