2

我有一个 FTP 服务器,我想从中下载本地目录中不存在的所有文件。

我试着做一个For Next,但我就是无法理解它。我尝试枚举文件,但由于对两个列表都这样做,我得到了一个错误。我认为该错误可能是由于使用本地列表中的单个枚举文件交叉检查在线文件引起的。我该如何消除这个问题?

链接到 FTPClient 类代码:

https://docs.google.com/file/d/0BxFwEuHe1g77TEw2ckZxVUlQdGM/edit?usp=sharing

所有代码:

          Dim ftp As New FTPclient("ftp://www.ahpg.zxq.net", "eg", "eg")

    Dim dirList As FTPdirectory = ftp.ListDirectoryDetail("/")
    Dim result As List(Of String) = ftp.ListDirectory("/")
    For Each line As String In result
        FTPLBX.Items.Add(line)
    Next
    Dim str As String
    Dim locstr As String
    Dim res_numer As IEnumerator
    res_numer = result.GetEnumerator()
    Dim loclist As List(Of String) = New List(Of String) _
                                     (System.IO.Directory.EnumerateFiles("C:/Program Files/Business Elements/Recent Files"))
    Dim LOC_Enum As IEnumerator
    LOC_Enum = loclist.GetEnumerator
    Do While LOC_Enum.MoveNext
        locstr = (LOC_Enum.Current)
    Loop
    Do While (res_numer.MoveNext)
        str = (res_numer.Current)
    Loop

    For Each str In loclist
        If Not loclist.Contains(str) = True Then
            My.Computer.Network.DownloadFile("ftp://www.ahpg.zxq.net/ftpfiles/" & str.ToString, _
                                             "C:/Program Files/Business Elements/Recent Files/" & str.ToString, "eg", "eg")
            MessageBox.Show("Done ")
        End If
    Next

End Sub
4

3 回答 3

1

如果它对你有用,我让它更容易一些。干得好:

    ' Your instance of FTPClient
    Dim ftp As New FTPclient("ftp://www.ahpg.zxq.net", "eg", "eg")

    ' The path to destination folder (Local directory)
    Dim localDir As String = "C:/Program Files/Business Elements/Recent Files/"

    ' Lists all the file in the given directory of FTP server
    For Each file As FTPfileInfo In ftp.ListDirectoryDetail("/").GetFiles

        Try
            ftp.Download(file, localDir & file.Filename)

            ' The FTPClient class throws exception if the 
            ' file already exists in destination directory
        Catch e As ApplicationException
            Console.WriteLine(e.Message)
        End Try
    Next file

注意 1:我从那里下载了FTPClient课程,CodeProject但它与您在问题中提供的课程几乎相同。

注意 2FTPClient如果文件存在于您的目标文件夹中,它本身会引发异常。因此,您无需费心比较文件。

注意 3:注意locadDir字符串末尾的斜杠。否则,该文件将下载到Business Element文件夹。

于 2013-04-01T12:22:00.027 回答
0

编写一个子方法,我们称之为 IsExistedInLocal 来检查来自 ftp 的文件是否已经在您的本地。如果是,请忽略并继续下一个;如果不是,请下载文件。我假设您知道所有细节,所以伪代码应该没问题

for each FTP_file in FTP_FilesList
  if not IsExistedInLocal(FTP_file) then
    download the file to local
  end if
next
于 2013-04-01T11:47:27.060 回答
0

在 for 循环中,您将获取列表中的每个条目,然后检查同一个列表是否没有该元素。您的 if 条件永远不会为真,也永远不会到达 DownloadFile 语句。

For Each str In loclist
    If Not loclist.Contains(str) = True Then
        My.Computer.Network.DownloadFile("ftp://www.ahpg.zxq.net/ftpfiles/" & str.ToString, _
                                         "C:/Program Files/Business Elements/Recent Files/" & str.ToString, "eg", "eg")
        MessageBox.Show("Done ")
    End If
Next
于 2013-04-04T18:16:20.453 回答