chupeman
chupeman

Reputation: 1115

Download file from ftp if newer or different

I am trying to download a file from an ftp site only if it's newer than my local file. Can anyone help how to incorporate to check for the file properties? right now it downloads the file, but just need if newer. The purpose is to update a .mdb with the contents of the file, so don't want to download file and run an update everytime my app runs, only if the file is different. This is the code I am using:

        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

Any help is appreciated

Upvotes: 1

Views: 5401

Answers (1)

Tony
Tony

Reputation: 56

Not sure if this answers your question but I am looking for a similar answer and came across this.

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

Look into LastWriteTime and you could save that time and check to see if it is a newer date then what is saved. You would have to also figure out how to download the file as a while(not familiar with the code maybe you are).

Upvotes: 1

Related Questions