我正在尝试获取特定目录中的文件列表,然后我想查看他们上次修改的日期。
初始请求正常:
FtpWebRequest request;
request = (FtpWebRequest)WebRequest.Create(new Uri(FtpPath));
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string FileNames = reader.ReadToEnd();
然后经过一些处理后,我选择了我感兴趣的文件并尝试检索他们的时间戳。以下内容发生在循环中:
request = (FtpWebRequest)WebRequest.Create(new Uri(FtpPath + Files[i]));
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
response = (FtpWebResponse)request.GetResponse();
responseStream = response.GetResponseStream();
reader = new StreamReader(responseStream);
FileDates = reader.ReadToEnd();
我的FileDates
变量永远不会被设置为任何内容。我喜欢把它打包成一个类以避免可怕的代码重复,但是现在我会满足于能够检索我感兴趣的数据。
答案 0 :(得分:0)
这是我的粗略解决方案:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(FtpPath));
reuest.Credentials = new NetworkCredential("user", "password");
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string FileNames = reader.ReadToEnd();
string[] Files = Regex.Split(FileNames,"\r\n");
现在我有一个包含所有文件名的数组
Dictionary<string, DateTime> Dates = new Dictionary<string, DateTime>();
for (int i = 0; i <= Files.Length - 1; i++)
{
request = (FtpWebRequest)WebRequest.Create(new Uri(FtpPath + Files[i]));
request.Credentials = new NetworkCredential("user", "password");
request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
response = (FtpWebResponse)request.GetResponse();
DateTime FileDate = response.LastModified;
Dates.Add(Files[i], FileDate);
}
一个字典,它将日期与每个文件相关联,以便进一步处理。我的初始解决方案的问题与我处理响应的方式有关。