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

问题描述

我必须从 FTP 服务器下载最新的文件.我知道如何从我的电脑下载最新的文件,但我不知道如何从 FTP 服务器下载.

I have to download the latest file from an FTP server. I know how download the latest file from my computer, but I don't how download from an FTP server.

如何从 FTP 服务器下载最新的文件?

How can I download the latest file from a FTP server?

这是我从我的电脑下载最新文件的程序

This is my program to download the latest file from my Computer

string startFolder = @"C:Usersuser3DesktopDocumentos XML";

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);

IEnumerable<System.IO.FileInfo> fileList =
    dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

IEnumerable<System.IO.FileInfo> fileQuerry =
    from file in fileList
    where file.Extension == ".txt"
    orderby file.CreationTimeUtc
    select file;

foreach (System.IO.FileInfo fi in fileQuerry)
{
    var newestFile =
    (from file in fileQuerry
     orderby file.CreationTimeUtc
     select new { file.FullName, file.Name })
     .First();
    textBox2.Text = newestFile.FullName;
}

好的,通过这段代码我知道最后一个文件的日期,但是我怎么知道这个文件的名称????????

OK, with this code I know the date of the last file, but How I know the name of this file????????

推荐答案

您必须检索远程文件的时间戳才能选择最新的.

You have to retrieve timestamps of remote files to select the latest one.

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

Unfortunately, there's no really reliable and efficient way to retrieve modification timestamps of all files in a directory using features offered by .NET framework, as it does not support the FTP MLSD command. The MLSD command provides a listing of remote directory in a standardized machine-readable format. The command and the format is standardized by RFC 3659.

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

Alternatives you can use, that are supported by .NET framework:

  • ListDirectoryDetails method (the FTP LIST command) to retrieve details of all files in a directory and then you deal with FTP server specific format of the details

DOS/Windows 格式:C# 类解析 WebRequestMethods.Ftp.ListDirectoryDe​​tails FTP 响应
*nix 格式:解析 FtpWebRequest ListDirectoryDe​​tails 行

DOS/Windows format: C# class to parse WebRequestMethods.Ftp.ListDirectoryDetails FTP response
*nix format: Parsing FtpWebRequest ListDirectoryDetails line

GetDateTimestamp 方法(FTP MDTM 命令)分别检索每个文件的时间戳.一个优点是响应由 RFC 3659 标准化为 YYYYMMDDHHMMSS[.sss].缺点是您必须为每个文件发送单独的请求,这可能效率很低.此方法使用 LastModified您提到的财产:

GetDateTimestamp method (the FTP MDTM command) to individually retrieve timestamps for each file. An advantage is that the response is standardized by RFC 3659 to YYYYMMDDHHMMSS[.sss]. A disadvantage is that you have to send a separate request for each file, what can be quite inefficient. This method uses the LastModified property that you mention:

const string uri = "ftp://example.com/remote/path/file.txt";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("{0} {1}", uri, response.LastModified);

或者,您可以使用支持现代 MLSD 命令的第 3 方 FTP 客户端实现.

Alternatively you can use a 3rd party FTP client implementation that supports the modern MLSD command.

例如 WinSCP .NET 程序集支持.

甚至还有一个针对您的特定任务的示例:下载最新文件.
该示例适用于 PowerShell 和 SFTP,但可以轻松转换为 C# 和 FTP:

There's even an example for your specific task: Downloading the most recent file.
The example is for PowerShell and SFTP, but translates to C# and FTP easily:

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

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

    // Get list of files in the directory
    string remotePath = "/remote/path/";
    RemoteDirectoryInfo directoryInfo = session.ListDirectory(remotePath);

    // Select the most recent file
    RemoteFileInfo latest =
        directoryInfo.Files
            .OrderByDescending(file => file.LastWriteTime)
            .First();

    // Download the selected file
    string localPath = @"C:localpath";
    session.GetFileToDirectory(latest.FullName, localPath);
}

(我是 WinSCP 的作者)

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

07-23 10:42
查看更多