3

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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
joyjumper
  • 51
  • 6

2 Answers2

2

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)
joyjumper
  • 51
  • 6
  • 1
    You might need to convert the time from UTC (as returned by FTP server) to local time. See [Convert UTC/GMT time to local time](http://stackoverflow.com/q/179940/850848). Thanks for sharing your solution and welcome to Stack Overflow! – Martin Prikryl Jul 23 '15 at 09:41
0
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
  • While this code may solve the question, [including an explanation](https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion – Muhammad Dyas Yaskur May 20 '20 at 01:29