在 C# 中获取 FTP 上的文件大小

编程入门 行业动态 更新时间:2024-10-24 01:54:08
本文介绍了在 C# 中获取 FTP 上的文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想获取 FTP 上文件的大小.

I want to get the size of a file on an FTP.

//Get File Size reqSize = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpPath + filePath)); reqSize.Credentials = new NetworkCredential(Username, Password); reqSize.Method = WebRequestMethods.Ftp.GetFileSize; reqSize.UseBinary = true; FtpWebResponse respSize = (FtpWebResponse)reqSize.GetResponse(); long size = respSize.ContentLength; respSize.Close();

我尝试了以下方法,但收到 550 错误.找不到文件/无法访问.但是,以下代码有效...

I have tried the following but get a 550 error. File not found / no access. However, the following code works...

reqTime = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpPath + filePath)); reqTime.Credentials = new NetworkCredential(Username, Password); reqTime.Method = WebRequestMethods.Ftp.GetDateTimestamp; reqTime.UseBinary = true; FtpWebResponse respTime = (FtpWebResponse)reqTime.GetResponse(); DateTime LastModified = respTime.LastModified; respTime.Close();

这对我不起作用的原因是我的 FTP 服务器不支持 SIZE 方法.

推荐答案

试试reqSize.Method = WebRequestMethods.Ftp.GetFileSize;而不是 GetDateTimestamp

Try reqSize.Method = WebRequestMethods.Ftp.GetFileSize; instead of GetDateTimestamp

这对我有用:

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://servername/filepath")); request.Proxy = null; request.Credentials = new NetworkCredential("user", "password"); request.Method = WebRequestMethods.Ftp.GetFileSize; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); long size = response.ContentLength; response.Close();

更多推荐

在 C# 中获取 FTP 上的文件大小

本文发布于:2023-11-07 22:41:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567638.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件大小   FTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!