【问题标题】:C# FTP get files that has been added todayC# FTP 获取今天添加的文件
【发布时间】:2014-12-04 11:30:01
【问题描述】:

我有一个 FTP,我想知道今天添加的文件。 (在我的业务规则中,文件没有更新,因此可以添加文件,然后根本无法修改或删除)。

我试过了:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://172.28.4.7/");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("{0} {1}", "ftp://172.28.4.7/", response.LastModified);
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

但是,正如预期的那样,在控制台中我得到了最后一次修改的日期。

您能帮我知道最后添加的文件吗?

【问题讨论】:

  • LastModified 不是指文件列表(想想:单个 DateTime 如何表示文件列表的时间戳?)。响应是原始文本,必须手动解析。另请参阅 MSDN 上的 blog post

标签: c# .net ftp ftpwebrequest


【解决方案1】:

您必须检索远程文件的时间戳以选择您想要的文件(今天的文件)。

不幸的是,没有真正可靠和有效的方法来使用 .NET 框架提供的功能来检索时间戳,因为它不支持 FTP MLSD 命令。 MLSD 命令以标准化的机器可读格式提供远程目录列表。命令和格式由RFC 3659标准化。

.NET 框架支持的您可以使用的替代方案:


或者,您可以使用支持现代MLSD 命令或可以在给定时间限制的情况下直接下载文件的第 3 方 FTP 客户端实现。

例如WinSCP .NET assembly 支持MLSDtime constraints

甚至还有一个针对您的特定任务的示例:How do I transfer new/modified files only?
该示例适用于 PowerShell,但可以轻松转换为 C#:

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "ftp.example.com",
    UserName = "username",
    Password = "password",
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Download today's times
    TransferOptions transferOptions = new TransferOptions();
    transferOptions.FileMask = "*>=" + DateTime.Today.ToString("yyyy-MM-dd");
    session.GetFiles(
        "/remote/path/*", @"C:\local\path\", false, transferOptions).Check();
}

(我是 WinSCP 的作者)

【讨论】:

    【解决方案2】:

    首先,您必须使用“ListDirectoryDe​​tails”获取所有目录详细信息:

    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails

    在 string[] 中获取响应。 通过检查字符串 [] 项目中的“DIR”文本来检查字符串 [] 是文件还是目录。

    从 string[] 获取文件名后,再次使用以下命令请求每个文件的“文件创建日期”:

     ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
    

    因此,您可以获得 FTP 服务器的文件添加日期。

    【讨论】:

      【解决方案3】:

      一个可能的同步解决方案(它可能对某人有用):

      数据容器类型:

          public class Entity
          {
              public DateTime uploadDate { get; set; }
              public string fileName { get; set; }
          }
      

      还有Lister小姐姐:

      public class FTPLister
          {
              private List<Entity> fileList = new List<Entity>();
      
              public List<Entity> ListFilesOnFTP(string ftpAddress, string user, string password)
              {
                  FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpAddress);
                  request.Method = WebRequestMethods.Ftp.ListDirectory;
      
                  request.Credentials = new NetworkCredential(user, password);
      
                  List<string> tmpFileList = new List<string>();
                  using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                  {
                      Stream responseStream = response.GetResponseStream();
                      StreamReader reader = new StreamReader(responseStream);
      
                      while (!reader.EndOfStream)
                      {
                          tmpFileList.Add(reader.ReadLine());
                      }
                  }
      
                  Uri ftp = new Uri(ftpAddress);
                  foreach (var f in tmpFileList)
                  {
                      FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri(ftp, f));
                      req.Method = WebRequestMethods.Ftp.GetDateTimestamp;
                      req.Credentials = new NetworkCredential(user, password);
      
                      using (FtpWebResponse resp = (FtpWebResponse)req.GetResponse())
                      {
                          fileList.Add(new Entity() { fileName=f, uploadDate=resp.LastModified });
                      }
                  }
      
                  fileList = fileList.Where(p => p.uploadDate>=DateTime.Today && p.uploadDate<DateTime.Today.AddDays(1)).ToList();
                  return fileList;
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-06
        • 1970-01-01
        • 2019-02-13
        • 1970-01-01
        相关资源
        最近更新 更多