2013-04-15 74 views
1

任何人都可以告訴我從哪裏可以得到FTP服務器上傳文件的時間?獲取FTP上傳文件的上傳時間

class listFiles 
{ 
    public static void Main(string[] args) 
    { 
     listFiles l = new listFiles(); 
     l.getFileList(ftpConnection,"test123","pass123"); //ftp url 
    } 
    private void getFileList(string FTPAddress, string username, string password) 
    { 
     List<string> files = new List<string>(); 

     try 
     { 
      //Create FTP request 
      FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress); 

      request.Method = WebRequestMethods.Ftp.ListDirectory; 
      request.Credentials = new NetworkCredential(username, password); 
      request.UsePassive = true; 
      request.UseBinary = true; 
      request.KeepAlive = true; 


      FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
      Stream responseStream = response.GetResponseStream(); 
      StreamReader reader = new StreamReader(responseStream); 

      while (!reader.EndOfStream) 
      { 
       //Application.DoEvents(); 
       // string fileType = reader.ReadLine(); 
       files.Add(reader.ReadLine()); 
      } 

      //Clean-up 
      reader.Close(); 
      responseStream.Close(); //redundant 
      response.Close(); 
     } 
     catch (Exception) 
     { 
      Console.WriteLine("There was an error connecting to the FTP Server"); 
     } 

     //If the list was successfully received, display it to the user 
     //through a dialog 
     if (files.Count != 0) 
     { 
      foreach (string file in files) 
      { 
       Console.WriteLine(file); 
      } 
     } 
    } 

回答

0

嘗試FtpWebResponse.LastModified Property

using (FtpWebResponse resp = (FtpWebResponse)request.GetResponse()) 
{ 
    var lastModified = resp.LastModified ; 
} 

你需要得到答覆爲每個文件,在當前的代碼,你已經文件的列表,通過使用文件名建立完整路徑FTP文件你需要找到創建日期。之後創建ftp請求到完整的ftp文件路徑。那麼你可以讀取響應對象的最後修改日期。

+0

嗨,謝謝Damith,但lastModified財產我得到的數據爲1/1/0001你能幫我更多這個? –

+0

我知道這是前一陣子,但確保檢索LastModifiedDate的請求有GetDateTimeStamp方法幫助我,當我得到一個空的日期。 'request.Method = WebRequestMethods.Ftp.GetDateTimestamp;' – Kai