joyjumper
joyjumper

Reputation: 51

FTP file last modified VB.NET

How do you get the date modified from a file on an FTP server in visual basic?

This is what I have so far:

Dim request = CType(WebRequest.Create(URL + ZipFile), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.GetDateTimestamp

I've tried a couple lines afterwards but none actually return the date.

Upvotes: 3

Views: 2605

Answers (2)

JHONN HERNANDEZ
JHONN HERNANDEZ

Reputation: 1

Private Function ServerDateFile(FtpFullPathFile) As Date
        Dim request As FtpWebRequest = WebRequest.Create(FtpFullPathFile)
        request.Credentials = New NetworkCredential( YourFtpUserName, YourPassWord)
        request.Method = WebRequestMethods.Ftp.GetDateTimestamp

        Dim dLastModified As Date = Date.MinValue 'Change for your default value or nothing

        Try
            Using response As FtpWebResponse = request.GetResponse()
                dLastModified = response.LastModified                   
            End Using
        Catch ex As WebException
            MsgBox(ex.Message, vbCritical)
        End Try

        Return dLastModified
    End Function

Upvotes: 0

joyjumper
joyjumper

Reputation: 51

Well I figured it out, but I'll leave this here since I couldn't find any other vb.net posts about this:

Imports System.Net
Imports System.Globalization

Dim request = CType(WebRequest.Create(URL + ZipFile), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.GetDateTimestamp
Dim response = CType(request.GetResponse(), FtpWebResponse)
Dim ServerDate = DateTime.ParseExact(response.StatusDescription.Substring(4,14),"yyyyMMddHHmmss",_
                                     Cultureinfo.InvariantCulture,DateTimeStyles.None)

Upvotes: 2

Related Questions