如果更新或不同,请从ftp下载文件

时间:2010-08-17 16:29:34

标签: vb.net visual-studio ftp

我试图从ftp站点下载文件,只要它比我的本地文件更新。任何人都可以帮助如何合并以检查文件属性?现在它下载文件,但只需要更新。目的是使用文件的内容更新.mdb,因此,只要文件不同,每次我的应用程序运行时都不要下载文件并运行更新。这是我正在使用的代码:

        Const localFile As String = "C:\version.xml"
    Const remoteFile As String = "/version.xml"
    Const host As String = "ftp://1.1.1.1"
    Const username As String = "user"
    Const password As String = "pwd"

    Dim URI As String = host & remoteFile
    Dim ftp As System.Net.FtpWebRequest = _
        CType(FtpWebRequest.Create(URI), FtpWebRequest)

    ftp.Credentials = New  _
        System.Net.NetworkCredential(username, password)

    ftp.KeepAlive = False

    ftp.UseBinary = True

    ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

    Using response As System.Net.FtpWebResponse = _
          CType(ftp.GetResponse, System.Net.FtpWebResponse)
        Using responseStream As IO.Stream = response.GetResponseStream

            Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
                Dim buffer(2047) As Byte
                Dim read As Integer = 0
                Do
                    read = responseStream.Read(buffer, 0, buffer.Length)
                    fs.Write(buffer, 0, read)
                Loop Until read = 0
                responseStream.Close()
                fs.Flush()
                fs.Close()
            End Using
            responseStream.Close()
        End Using
        response.Close()
    End Using

感谢任何帮助

1 个答案:

答案 0 :(得分:1)

不确定这是否能解答您的问题,但我正在寻找类似的答案,并且遇到了这个问题。

http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx

查看LastWriteTime,您可以节省该时间并检查它是否是更新的日期然后保存的内容。您还必须弄清楚如何下载文件一段时间(不熟悉您的代码)。