VB.NET 迭代文件和目录

发布于 2024-08-21 18:46:07 字数 1757 浏览 2 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔少女心 2024-08-28 18:46:07

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

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 相同。

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.

落花浅忆 2024-08-28 18:46:07

您必须分别处理目录和文件,但这仍然非常简单。

System.IO.DirectoryInfo 包含指定目录中的列表文件和子目录。查找属性目录和文件。

您需要做的是指定一个函数,该函数将首先读取 DirectoryInfo 中的所有文件,然后读取目录。对于文件,您可以对它执行您想要执行的操作(我认为是上传),一旦您点击目录,就使用子目录作为参数递归调用您的函数。这将遍历所有子目录和文件。

伪代码(我称其为伪代码,因为我不知道确切的语法)

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

希望有所帮助。

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)

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.

寂寞美少年 2024-08-28 18:46:07

Have a look at the DirectoryInfo class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文