0

Using WebClient, how can I download a file from a FTP server with the filetime from server?

using (WebClient client = new WebClient()) {
    client.DownloadFile("ftp://ftp123.abc.com/xyz/file.txt", "file.txt");
}

The code above creates a new file, so its timestamp is at the time of the download.

The question is how to retrieve the timestamp from the server file.

joe
  • 8,344
  • 9
  • 54
  • 80
  • @Daniel A. White: The question is NOT how to chaneg the local file time - the question is how to retrieve the file time from the FTP server! – joe Mar 24 '15 at 13:16
  • @joe your question was not very clear. – Daniel A. White Mar 24 '15 at 13:18
  • @DanielA.White: By the time it was closed, there was not a chance that it was even read. – joe Mar 24 '15 at 13:19
  • Why do you have this requirement to use only the WebClient class? – Steve Mar 24 '15 at 13:19
  • @Steve: You recommend something else? The goal is to download the file with its original time stamp, no matter now. – joe Mar 24 '15 at 13:19
  • 3
    `FtpWebResponse.LastModified` https://msdn.microsoft.com/en-us/library/system.net.ftpwebresponse.lastmodified(v=vs.110).aspx – Alex K. Mar 24 '15 at 13:20

1 Answers1

1

From MSDN

public static void GetDateTimestampOnServer (Uri serverUri)
    {
        if (serverUri.Scheme != Uri.UriSchemeFtp)
        {
            throw new ArgumentException("Scheme Must match Ftp Uri Scheme");
        }

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create (serverUri);
        request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
        FtpWebResponse response = (FtpWebResponse)request.GetResponse ();
        Console.WriteLine ("{0} {1}",serverUri,response.LastModified);
    }
Jason Portnoy
  • 757
  • 8
  • 23