问题描述
我正在使用 System.Net.FtpWebRequest
类,我的代码如下:
I'm using the System.Net.FtpWebRequest
class and my code is as follows:
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 上提供的示例,但我找不到更详细的内容.
This is based off of the examples provided on MSDN but I couldn't find anything more detailed.
我将文件夹中的所有文件名存储在 names
中,但是我现在如何遍历每个文件名并检索它们的日期?我想检索日期,以便找到最新的文件.谢谢.
I'm storing all the filenames in the folder in names
but how can I now iterate through each of those and retrieve their dates? I want to retrieve the dates so I can find the newest files. Thanks.
推荐答案
WebRequestMethods.Ftp.ListDirectory
返回 FTP 目录中所有文件的短列表".这种类型的列表只会提供文件名 - 而不是文件的其他详细信息(如权限或上次修改日期).
WebRequestMethods.Ftp.ListDirectory
returns a "short listing" of all the files in an FTP directory. This type of listing is only going to provide file names - not additional details on the file (like permissions or last modified date).
改用 WebRequestMethods.Ftp.ListDirectoryDetails
.此方法将返回 FTP 服务器上的一长串文件.将此列表检索到 names
变量后,您可以根据行尾字符将 names
变量拆分为一个数组.这将导致每个数组元素成为一个文件(或目录)名称列表,其中包括权限、上次修改日期所有者等...
Use WebRequestMethods.Ftp.ListDirectoryDetails
instead. This method will return a long listing of files on the FTP server. Once you've retrieved this list into the names
variable, you can split the names
variable into an array based on an end of line character. This will result in each array element being a file (or directory) name listing that includes the permissions, last modified date owner, etc...
此时,您可以遍历该数组,检查每个文件的最后修改日期,并决定是否下载该文件.
At this point, you can iterate over this array, examine the last modified date for each file, and decide whether to download the file.
希望对你有帮助!!
这篇关于检索文件的创建日期 (FTP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!