Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get File Size On An FTP in C#

Tags:

c#

size

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();

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();

EDIT: The reason this isn't working for me is that my FTP Server does not support the SIZE method.

like image 757
Jason Avatar asked Nov 14 '10 02:11

Jason


People also ask

How do I know my FTP size?

Then look at the queue pane and below it (on the status bar) you should see a message indicating the queue size. Very good solution. This way, you can get the total number of files and the total size without Downloading any file. The FTP command used by Filezilla is MLSD (recursively) if anybody is curious about it.

How to get file size from ftp in c#?

public static long GetFtpFileSize(Uri requestUri, NetworkCredential networkCredential) { //Create ftpWebRequest object with given options to get the File Size. var ftpWebRequest = GetFtpWebRequest(requestUri, networkCredential, WebRequestMethods. Ftp. GetFileSize); try { return ((FtpWebResponse)ftpWebRequest.

How do I see file size in FileZilla?

Re: How can I view each file's size using FileZilla? The file size is displayed in the file size column for every file, aggregate size is displayed in the status bar below the file list.

Does FTP have a file size limit?

Answer: There is not a limit on the size of the file. But keep in mind that the bigger the file the longer it will take to be transferred. However, either a ReadTimeout or OutOfMemory message may appear, depending on how many files are on the ftp server, and how large the files are.


2 Answers

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

This worked for me:

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();
like image 113
bbosak Avatar answered Oct 14 '22 16:10

bbosak


I tried to get file size while downloading it. Both, given answers doesn't work for me, so I made this, using StatusDescription property of FtpWebResponse with DownloadFile method:

int openingBracket = response.StatusDescription.IndexOf("(");
int closingBracket = response.StatusDescription.IndexOf(")");                     
var temporarySubstring = response.StatusDescription.Substring(openingBracket+1, closingBracket - openingBracket);
var fileSize = temporarySubstring.Substring(0, temporarySubstring.IndexOf(" "));

I suppose there is better way to do this with RegEx.

like image 32
Przemysław Wojciech Witczak Avatar answered Oct 14 '22 15:10

Przemysław Wojciech Witczak