【问题标题】:Download files from FTP server in C# to local folder with modification date greater than specified将文件从 C# 中的 FTP 服务器下载到修改日期大于指定日期的本地文件夹
【发布时间】:2017-06-20 19:03:12
【问题描述】:

我从事 C# Web 应用程序,需要使用 FTP 将文件下载到本地文件夹。这些图像的修改日期必须大于我指定的日期。

代码:

public static List<FTPLineResult> GetFilesListSortedByDate(string ftpPath, Regex nameRegex, DateTime cutoff, System.Security.Cryptography.X509Certificates.X509Certificate cert)
{
    List<FTPLineResult> output = new List<FTPLineResult>();

    if (cert != null)
    {
        FtpWebRequest request = FtpWebRequest.Create(ftpPath) as FtpWebRequest;
        request.Credentials = new NetworkCredential("unm", "pwd");
        request.ClientCertificates.Add(cert);

        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;
}

我必须使用证书 (.p12)。
我该怎么做?

【问题讨论】:

    标签: 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 files created in 2017-06-15 and later
        TransferOptions transferOptions = new TransferOptions();
        transferOptions.FileMask = "*>=2017-06-15";
        session.GetFiles(
            "/remote/path/*", @"C:\local\path\", false, transferOptions).Check();
    }
    

    虽然对于 Web 应用程序,WinSCP 可能不是最好的解决方案。您也许可以找到另一个具有类似功能的第三方库。


    WinSCP 还支持使用客户端证书进行身份验证。见SessionOptions.TlsClientCertificatePath。但这确实是一个单独的问题。

    (我是 WinSCP 的作者)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      相关资源
      最近更新 更多