Retrieving creation date of file (FTP)

Radu picture Radu · Dec 15, 2010 · Viewed 38.1k times · Source

I'm using the System.Net.FtpWebRequest class and my code is as follows:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://example.com/folder");
request.Method = WebRequestMethods.Ftp.ListDirectory;

request.Credentials = new NetworkCredential("username", "password");

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

string names = reader.ReadToEnd();

reader.Close();
response.Close();

This is based off of the examples provided on MSDN but I couldn't find anything more detailed.

I'm storing all the filenames in the folder in names but how can I now iterate through each of those and retrieve their dates? I want to retrieve the dates so I can find the newest files. Thanks.

Answer

Jack0fshad0ws picture Jack0fshad0ws · Sep 27, 2011

This seems to work just fine http://msdn.microsoft.com/en-us/library/system.net.ftpwebresponse.lastmodified(v=VS.90).aspx

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