我如何下载FTP服务器的最旧文件? [英] How can I download the oldest file of an FTP server?

查看:189
本文介绍了我如何下载FTP服务器的最旧文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  FtpWebRequest请求=(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);

字符串名称= reader.ReadLine();

textBox12.Text = names;


解决方案


如何下​​载FTP服务器最旧的文件?


使用 WebRequestMethods.Ftp.ListDirectoryDe​​tails
$ b

这将发出一个FTP LIST命令,请求获取单个请求中文件的详细信息。这不会让事情变得简单,因为您必须解析这些行,并且没有标准格式。



根据ftp服务器的不同,它可能会返回行以这样的格式:

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



  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

甚至可以是另一种格式。

此博客文章用于解析ListDirectoryDe​​tails的FtpwebRequest响应的示例代码提供了一个处理多种格式的例子。



如果您知道格式是什么,只需为它创建一个自定义的最小行解析器。



<使用 WebRequestMethods.Ftp.ListDirectory WebRequestMethods.Ftp.GetDateTimestamp



这很容易,但缺点是它需要您提交几个请求才能找到目录条目的最后修改日期。



这会给你一个只包含名字的文件和目录条目列表,这是比较容易分析的。

 公共静态IEnumerable< string> ListDirectory(字符串uri,NetworkCredential凭证)
{
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();


$ / code>

然后对于每个文件你可以得到最后的修改日期通过为每个文件发出一个请求:

pre $ public static DateTime GetLastModified(string fileUri,NetworkCredential credentials)
{
//错误检查省略
var request = FtpWebRequest.Create(fileUri);
request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
request.Credentials = credentials;
using(var response =(FtpWebResponse)request.GetResponse())
return response.LastModified;

$ / code>

现在您可以简单地执行以下操作来获取文件列表修改日期。

  var credentials = new NetworkCredential(Igor,); 
var filesAndDates = ListDirectory(ftp://192.168.47.1/DocXML,凭证)
.Select(fileName => new {
FileName = fileName,
LastModified = GetLastModified(ftp://192.168.47.1/DocXML/+文件名,凭证)
})
.ToList();
//找到最旧的条目。
var oldest = filesAndDates.OrderBy(x => x.LastModified).FirstOrDefault();


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; 

解决方案

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();

这篇关于我如何下载FTP服务器的最旧文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆