如果文件是在过去一小时内创建的,请从FTP下载文件 [英] Download files from FTP if they are created within the last hour

查看:54
本文介绍了如果文件是在过去一小时内创建的,请从FTP下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从FTP上的特定文件夹下载文件,但仅需要在最后一小时内创建文件的文件即可.因此,基本上,我需要列出该文件夹中的所有文件,然后仅下载时间戳,它们的时间戳在距执行时间不超过一小时的时间内没有变化.关于如何解析FTP上的时间戳记的任何想法?我不能使用任何第三方FTP客户端库.

I need to download files from specific folder on FTP, but only the ones with creation time within last hour. So basically I need to list all files from that folder, and then download only the ones for which time stamp doesn't differ for more then one hour from execution time. Any ideas on how to parse Time Stamp for file on FTP? I can not use any 3rd-party FTP client libraries.

推荐答案

这是我现在想出的.当然,有一些更优雅的方法可以解决这个问题,但是...

This is what I came up with for now. Sure there is some more elegant way to handle this but...

foreach (var fileName in filesNamesFromFtpFolder)
        {
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(@"ftp://" + host + @"/" + folder + @"/" + fileName);

            request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
            request.Proxy = null;

            using (FtpWebResponse resp = (FtpWebResponse)request.GetResponse())
            {
                if (DateTime.Now.Subtract(TimeSpan.FromMinutes(60)) < resp.LastModified)
                {
                    //download this file...
                }
            }
        }

这篇关于如果文件是在过去一小时内创建的,请从FTP下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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