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


转载于:https://www.cnblogs.com/h2appy/archive/2008/09/12/1290015.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C#中常用的FTP文件传输命令: 1.连接FTP服务器 ```csharp FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/"); request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; request.Credentials = new NetworkCredential("username", "password"); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); ``` 2.上传文件FTP服务器 ```csharp string filePath = @"C:\example.txt"; string ftpPath = "ftp://ftp.example.com/example.txt"; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpPath); request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential("username", "password"); byte[] fileContents = File.ReadAllBytes(filePath); request.ContentLength = fileContents.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(fileContents, 0, fileContents.Length); } FtpWebResponse response = (FtpWebResponse)request.GetResponse(); ``` 3.从FTP服务器下载文件 ```csharp string ftpPath = "ftp://ftp.example.com/example.txt"; string filePath = @"C:\example.txt";FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpPath); request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential("username", "password"); using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) using (Stream responseStream = response.GetResponseStream()) using (Stream fileStream = File.Create(filePath)) { responseStream.CopyTo(fileStream); } ``` 4.删除FTP服务器上的文件 ```csharp string ftpPath = "ftp://ftp.example.com/example.txt"; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpPath); request.Method = WebRequestMethods.Ftp.DeleteFile; request.Credentials = new NetworkCredential("username", "password"); FtpWebResponse response = (FtpWebResponse)request.GetResponse();```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值