简体   繁体   English

多个FTP请求-C#

[英]Multiple FTP requests - C#

I'm trying to get a listing of files in a specific directory and then I want to check their last modified dates. 我正在尝试获取特定目录中文件的列表,然后想查看它们的最后修改日期。

Initial request works fine: 初始请求工作正常:

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();

Then after some processing, I pick the files that I'm interested in and attempt to retrieve their time stamps. 然后,经过一些处理,我选择了我感兴趣的文件,并尝试检索它们的时间戳。 The following happens in a loop: 循环发生以下情况:

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();

My FileDates variable never gets set to anything. 我的FileDates变量从未设置为任何值。 I'd love to package this in a class to avoid the horrible code duplication but for now I would settle for being able to retrieve the data that I'm interested in. 我很乐意将其打包在一个类中,以避免可怕的代码重复,但是现在,我会满足于能够检索我感兴趣的数据。

Here is my rough solution: 这是我的粗略解决方案:

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");

Now I've got an array of all the filenames 现在我有了所有文件名的数组

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

And a dictionary that associates a date with each file for further processing. 以及将日期与每个文件关联以进行进一步处理的字典。 The problem with my initial solution had something to do with the way I was handling the response. 我最初的解决方案的问题与我处理响应的方式有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM