2010-12-16 54 views
0

我想獲取特定目錄中的文件列表,然後我想檢查他們的最後修改日期。多個FTP請求 - C#

初始請求正常工作:

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

有沒有一個/在FtpPath的結尾? – 2010-12-16 21:52:47

+0

@Scott Chamberlain是的。 – Radu 2010-12-16 22:34:56

回答

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

而這與相關聯的每個文件進行進一步的處理日期的字典的數組。我最初的解決方案的問題與我處理響應的方式有關。