Search code examples
c#webclientfiletime

How to download file with correct timestamp?


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.


Solution

  • 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);
        }