How can I download the oldest file of an FTP server?
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://192.168.47.1/DocXML");
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential("Igor", "");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string names = reader.ReadLine();
textBox12.Text = names;
Using
WebRequestMethods.Ftp.ListDirectoryDetails
This will issue an FTP LIST command with a request to get the details on the files in a single request. This does not make things easy though because you will have to parse those lines, and there is no standard format for them.
Depending on the ftp server, it may return lines in a format like this:
Or
Or even another format.
This blog post "Sample code for parsing FtpwebRequest response for ListDirectoryDetails" provides an example of handling several formats.
If you know what the format is, just create a custom minimal line parser for it.
Using
WebRequestMethods.Ftp.ListDirectory
withWebRequestMethods.Ftp.GetDateTimestamp
This is easier, but the downside is that it requires you to submit several requests to find out the last modification dates for the directory entries.
This will get you a list of file and directory entries with names only, that is easier to parse.
Then for each file you can get the last modification date by issuing a request per file:
Now you can simply do the following to get a list of files with their last modification date.