butter7372 发表于 2015-5-27 10:37:53

“WebRequestMethods.Ftp.DownloadFile不能保留文件最后修改时间”的解决方法

  WebRequestMethods.Ftp.DownloadFile不能保留文件最后修改时间这个属,WebRequestMethods.Ftp.ListDirectoryDetails中只有月日时分,通过分析它也不能解决问题。
  
  要想保留文件最后修改时间我们需要手动获取时间,然后进行设置。代码如下:
  
  
  

      public static DateTime GetDateTimestamp(string remoteFile, bool useSSL, string username, string password)
      {
            Uri uri = new Uri(remoteFile);
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
            request.Credentials = new NetworkCredential(username, password);
            request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
            request.UseBinary = true;
            request.UsePassive = false;
            if (useSSL)
            {
                //下面4行代码用于支持显示SSL(explicit SSL),.NET2.0中的FtpWebRequest不支持
                //隐式SSL(implicit SSL)。如果不用SSL,注释掉它们即可。
                request.EnableSsl = true;
                ServicePointManager.ServerCertificateValidationCallback =
                  delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
                  { return true; };
            }
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            return response.LastModified;
      }  
页: [1]
查看完整版本: “WebRequestMethods.Ftp.DownloadFile不能保留文件最后修改时间”的解决方法