在C#中基于日期时间获取FTP文件详细信息 [英] Get FTP file details based on datetime in C#

查看:386
本文介绍了在C#中基于日期时间获取FTP文件详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我想基于某个特定的日期时间从FTP服务器获取文件详细信息,而无需使用任何第三方.

Question: I want to get file Details from FTP server based on some specific datetime without using any 3rd party.

问题::我的FTP服务器包含1000个文件,因此获取所有文件,然后进行过滤需要花费时间.

Problem : My FTP server contains 1000s of files so getting all files and after that filtering it takes time.

有什么更快的方法吗?

string ftpPath = "ftp://directory/";

// Some expression to match against the files...do they have a consistent 
// name? This example would find XML files that had 'some_string' in the file 

Regex matchExpression = new Regex("^test.+\.xml$", RegexOptions.IgnoreCase);

// DateFilter
DateTime cutOff = DateTime.Now.AddDays(-10);

List<ftplineresult> results = FTPHelper.GetFilesListSortedByDate(ftpPath, matchExpression, cutOff);

public static List<FTPLineResult> GetFilesListSortedByDate(string ftpPath, Regex nameRegex, DateTime cutoff)
{
    List<FTPLineResult> output = new List<FTPLineResult>();
    FtpWebRequest request = FtpWebRequest.Create(ftpPath) as FtpWebRequest;
    ConfigureProxy(request);
    request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    FtpWebResponse response = request.GetResponse() as FtpWebResponse;
    StreamReader directoryReader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
    var parser = new FTPLineParser();
    while (!directoryReader.EndOfStream)
    {
        var result = parser.Parse(directoryReader.ReadLine());
        if (!result.IsDirectory && result.DateTime > cutoff && nameRegex.IsMatch(result.Name))
        {
            output.Add(result);
        }
    }
    // need to ensure the files are sorted in ascending date order
    output.Sort(
        new Comparison<FTPLineResult>(
            delegate(FTPLineResult res1, FTPLineResult res2)
            {
                return res1.DateTime.CompareTo(res2.DateTime);
            }
        )
    );
    return output;
}

推荐答案

我有一个替代解决方案,可以使用FluentFTP来实现我的功能.

说明:

我正在从FTP(具有读取权限要求)下载具有相同文件夹结构的文件.

I am downloading the files from FTP (Read permission reqd.) with same folder structure.

因此,每次运行作业/服务时,我都可以将相同文件(完整路径)存入物理路径,如果不存在,则可以将其视为新文件.而且Ii可以为此做一些动作,也可以下载.

So everytime the job/service runs I can check into the physical path same file(Full Path) exists or not If not exists then it can be consider as a new file. And Ii can do some action for the same and download as well.

这只是一个替代解决方案.

Its just an alternative solution.

代码更改:

 private static void GetFiles()
 {
    using (FtpClient conn = new FtpClient())
    {
        string ftpPath = "ftp://myftp/";

        string downloadFileName = @"C:\temp\FTPTest\";

        downloadFileName +=  "\\";

        conn.Host = ftpPath;
        //conn.Credentials = new NetworkCredential("ftptest", "ftptest");
        conn.Connect();

        //Get all directories

        foreach (FtpListItem item in conn.GetListing(conn.GetWorkingDirectory(),
            FtpListOption.Modify | FtpListOption.Recursive))
        {
            // if this is a file
            if (item.Type == FtpFileSystemObjectType.File)
            {
                string localFilePath = downloadFileName + item.FullName;

                //Only newly created files will be downloaded.
                if (!File.Exists(localFilePath))
                {
                    conn.DownloadFile(localFilePath, item.FullName);
                    //Do any action here.
                    Console.WriteLine(item.FullName);
                }
            }
        }
    }
}

这篇关于在C#中基于日期时间获取FTP文件详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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