本文介绍了从FTP下载文件,如果它不能在本地文件给定的名单存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我试着做一个对于下一步但我不能让我的头周围。我试图枚举文件,但因为它做给两个列表的结果,我得到了一个错误。我认为该错误可能会从交叉检查的在线文件从本地列表中的单个枚举文件引起的。我该如何解决这个问题?

链接到FtpClient的类code:

https://docs.google.com/file/d/ 0BxFwEuHe1g77TEw2ckZxVUlQdGM /编辑?USP =共享

所有code:

 昏暗FTP作为新FtpClient的(ftp://www.ahpg.zxq.net,如,如)

    昏暗dirList作为FTPdirectory = ftp.ListDirectoryDe​​tail(/)
    昏暗的结果为列表(字符串)= ftp.ListDirectory(/)
    对于每行字符串中的结果
        FTPLBX.Items.Add(线)
    下一个
    昏暗str作为字符串
    昏暗locstr作为字符串
    昏暗res_numer作为的IEnumerator
    res_numer = result.GetEnumerator()
    昏暗loclist方式列表(字符串)=新名单(串)_
                                     (System.IO.Directory.EnumerateFiles(C:/ Program Files文件/业务元素/最近文件))
    昏暗LOC_Enum作为的IEnumerator
    LOC_Enum = loclist.GetEnumerator
    做,而LOC_Enum.MoveNext
        locstr =(LOC_Enum.Current)
    循环
    做,而(res_numer.MoveNext)
        STR =(res_numer.Current)
    循环

    对于每一个海峡在loclist
        如果不loclist.Contains(STR)= true,那么
            My.Computer.Network.DownloadFile(ftp://www.ahpg.zxq.net/ftpfiles/与& str.ToString,_
                                             C:/ Program Files文件/业务元素/最近的文件/&放大器; str.ToString,例如,比如)
            的MessageBox.show(完成)
        结束如果
    下一个

结束小组
 

解决方案

我做到了,如果你的作品更容易一些。在这里你去:

 '你FtpClient的实例
    昏暗的FTP作为新FtpClient的(ftp://www.ahpg.zxq.net,如,如)

    的路径目标文件夹(本地目录)
    昏暗LOCALDIR作为字符串=C:/ Program Files文件/业务元素/最近的文件/

    列出FTP服务器的指定目录下的所有文件
    对于每个文件FTPfileInfo在ftp.ListDirectoryDe​​tail(/)。的GetFiles

        尝试
            ftp.Download(文件,LOCALDIR和放大器; file.Filename)

            该FtpClient的类抛出异常,如果
            文件已经存在于目标目录
        卡子E作为ApplicationException的
            Console.WriteLine(e.Message)
        结束尝试
    接下来文件
 

注1 :我下载了的从的但它几乎是一样的,你在问题中提供的。

注2 :在本身抛出一个异常。所以,你不必费心比较文件。

注意3 :注意尾随的削减的的的 locadDir 的字符串的结尾。如果没有这些,该文件将被下载到的商业元素的文件夹。

I have an FTP server from which I want to download all the files that do not exist in my local directory.

I tried doing a For Next but I just can't get my head around it. I tried enumerating the files but as a result of doing it to both lists, I got an error. I think the error might be caused from cross checking the online files with an individual enumerated file from the local list. How do I eliminate this problem?

Link to FTPClient Class Code:

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

All Code:

          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
解决方案

I made it a little easier if it works for you. Here you go:

    ' 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

Note 1: I downloaded the FTPClient class from CodeProject but it's almost the same as the one you provided in the question.

Note 2: The FTPClient itself throws an exception if the file exists in your destination folder. So you don't need to bother comparing the files.

Note 3: Notice the trailing slash at the end of locadDir string. Without that, the file will be downloaded to the Business Element folder.

这篇关于从FTP下载文件,如果它不能在本地文件给定的名单存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 22:46