How can I download the oldest file of an FTP serve

2020-04-30 03:22发布

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; 

1条回答
相关推荐>>
2楼-- · 2020-04-30 04:01

How can I download the oldest file of an FTP server?

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:

08-10-11  12:02PM       <DIR>          Version2
06-25-09  02:41PM            144700153 image34.gif
06-25-09  02:51PM            144700153 updates.txt
11-04-10  02:45PM            144700214 digger.tif

Or

d--x--x--x    2 ftp      ftp          4096 Mar 07  2002 bin
-rw-r--r--    1 ftp      ftp        659450 Jun 15 05:07 TEST.TXT
-rw-r--r--    1 ftp      ftp     101786380 Sep 08  2008 TEST03-05.TXT
drwxrwxr-x    2 ftp      ftp          4096 May 06 12:24 dropoff

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 with WebRequestMethods.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.

public static IEnumerable<string> ListDirectory(string uri, NetworkCredential credentials)
{
    var request = FtpWebRequest.Create(uri);
    request.Method = WebRequestMethods.Ftp.ListDirectory;
    request.Credentials = credentials;
    using (var response = (FtpWebResponse)request.GetResponse())
    using (var stream = response.GetResponseStream())
    using (var reader = new StreamReader(stream, true))
    {
        while (!reader.EndOfStream)
            yield return reader.ReadLine();
    }
}

Then for each file you can get the last modification date by issuing a request per file:

public static DateTime GetLastModified(string fileUri, NetworkCredential credentials) 
{
    // error checking omitted
    var request = FtpWebRequest.Create(fileUri);
    request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
    request.Credentials = credentials;
    using (var response = (FtpWebResponse)request.GetResponse())
        return response.LastModified;
}

Now you can simply do the following to get a list of files with their last modification date.

var credentials = new NetworkCredential("Igor", "");
var filesAndDates = ListDirectory("ftp://192.168.47.1/DocXML", credentials)
    .Select(fileName => new {
        FileName = fileName,
        LastModified = GetLastModified("ftp://192.168.47.1/DocXML/" + fileName, credentials)
    })
    .ToList();
// find the oldest entry.
var oldest = filesAndDates.OrderBy(x => x.LastModified).FirstOrDefault();
查看更多
登录 后发表回答