检索文件的创建日期(FTP)

我正在使用System.Net.FtpWebRequest类,我的代码如下:

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://example.com/folder"); request.Method = WebRequestMethods.Ftp.ListDirectory; request.Credentials = new NetworkCredential("username", "password"); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); string names = reader.ReadToEnd(); reader.Close(); response.Close(); 

这是基于MSDN上提供的示例,但我找不到更详细的内容。

我将文件夹中的所有文件名存储在names但我现在如何遍历每个文件names并检索其日期? 我想检索日期,以便找到最新的文件。 谢谢。

WebRequestMethods.Ftp.ListDirectory返回FTP目录中所有文件的“简短列表”。 此类型的列表仅提供文件名 – 而不是文件的其他详细信息(如权限或上次修改日期)。

请改用WebRequestMethods.Ftp.ListDirectoryDetails 。 此方法将返回FTP服务器上的长文件列表。 将此列表检索到names变量后,可以根据行尾字符将names变量拆分为数组。 这将导致每个数组元素都是一个文件(或目录)名称列表,其中包括权限,上次修改日期所有者等…

此时,您可以遍历此数组,检查每个文件的上次修改日期,并决定是否下载该文件。

我希望这有帮助!!

这似乎工作得很好http://msdn.microsoft.com/en-us/library/system.net.ftpwebresponse.lastmodified(v=VS.90).aspx

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create (serverUri); request.Method = WebRequestMethods.Ftp.GetDateTimestamp; FtpWebResponse response = (FtpWebResponse)request.GetResponse (); Console.WriteLine ("{0} {1}",serverUri,response.LastModified); 

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

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

  • ListDirectoryDetails方法(FTP LIST命令)检索目录中所有文件的详细信息,然后处理FTP服务器特定格式的详细信息(* nix格式类似于ls * nix命令是最常见的,缺点是格式可能会随着时间而改变,因为较新的文件使用“May 8 17:48”格式,而旧文件使用“Oct 18 2009”格式。

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

  • GetDateTimestamp方法(FTP MDTM命令)单独检索每个文件的时间戳。 优点是响应由RFC 3659标准化为YYYYMMDDHHMMSS[.sss] 。 缺点是您必须为每个文件发送单独的请求,这可能是非常低效的。

     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命令的第三方FTP客户端实现。

例如, WinSCP .NET程序集支持它。

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

 // 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:\local\path\"; string sourcePath = RemotePath.EscapeFileMask(remotePath + latest.Name); session.GetFiles(sourcePath, localPath).Check(); } 

(我是WinSCP的作者)

首先,您需要在文件名分隔符上使用String.Split拆分名称。 然后遍历所有字符串并导航目录