博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#获取ftp文件最后修改时间

Posted on 2014-01-09 08:58  hyruur  阅读(3822)  评论(0编辑  收藏  举报

public static DateTime GetFileModifyDateTime(string ftpServerIP,string ftpFolder,string ftpUserID,string ftpPwd, string fileName)

  {
  FtpWebRequest reqFTP=null;
  try
  {
  if (ftpFolder != "")
  {
  ftpFolder = ftpFolder.Replace("/", "").Replace("\\", "");
  ftpFolder = "/" + ftpFolder;
  }
  string ftpPath = "ftp://" + ftpServerIP + ftpFolder + "/" + fileName;

  reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));
    
  reqFTP.UseBinary = true;
  //reqFTP.UsePassive = false;
  reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPwd);

  reqFTP.Method = WebRequestMethods.Ftp.GetDateTimestamp;

  FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

  DateTime dt = response.LastModified;
    
  response.Close();
  response = null;
  reqFTP = null;
  return dt;
  }
  catch (Exception ex)
  {
  throw ex;
  }  
  }