1

I am currently having a problem where WebRequestMethods.Ftp.GetFileSize is throwing an exception

File unavailable (eg, file not found, can not access the file)

I have researched the issue and read that the issue is occurring because my FTP server doesn't support the function. I later contacted my web server provider which they confirmed that the function is not supported unless I purchase one of their VPS plans (I am currently on shared hosting).

// get size of file
FtpWebRequest sizeRequest = (FtpWebRequest)WebRequest.Create("ftp://ftp.domain.com/test.txt");
sizeRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; // or GetFileSize();
sizeRequest.Credentials = new NetworkCredential("user", "password");
sizeRequest.UsePassive = true;
sizeRequest.UseBinary = true;
sizeRequest.KeepAlive = true;
int size = (int)sizeRequest.GetResponse().ContentLength;

My question is that are there solutions to the GetFileSize method? I have looked into ListDirectoryDetails. However since I only need one piece of data, I would need to parse the rest of the data which after looking into how to do that, It was going to take me a while to sufficiently understand what code I need to write since I am only a novice C# programmer.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

1 Answers1

0

For sure, FTP protocol does not have two methods for retrieving a file size. And even if it had, the other method would be disabled on your server the same way the SIZE is.


All the other options involve parsing a directory listing.

You can use:

  • MLST – to retrieve file attributes of a specific file in machine readable format. While this is the best alternative, if you want to retrieve a size of one specific file, it is not supported by FtpWebRequest. You would have to use another FTP library.

    For example with my WinSCP .NET assembly, you can use Session.GetFileInfo:

    session.GetFileInfo("/remote/path/file.txt").Length
    
  • MLSD – to retrieve file attributes of a all files in a directory in a machine readable format. Again, not supported by FtpWebRequest.

  • LIST – to retrieve directory listing in an undefined format. This is supported by FtpWebRequest, as you know, via the ListDirectoryDetails method.

    For an example of parsing in C# see my answer to:
    Parsing FtpWebRequest ListDirectoryDetails line

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Ok, my only concern is that I am not sure if MLSD or MLST, which is not supported by FtpWebRequest have options to enable SSL encryption (I know FtpWebRequest does), which was my next question. Will SSL encryption make any difference when it comes to security? Security for me is a big concern when it comes to FTP and information being available to the public (I have an active SSL running on my web server). – Christopher Harkins Oct 02 '19 at 08:58
  • Sorry, your comment/question makes little sense to me. Encryption is FTP protocol thing, not related to specific commands. Consider asking a separate question for it and elaborate on your concerns. – Martin Prikryl Oct 02 '19 at 09:14
  • I believe that your current question is answered. – Martin Prikryl Oct 02 '19 at 09:14
  • maybe I could have been more specific, I was using this link: [link](https://learn.microsoft.com/en-us/dotnet/api/system.net.ftpwebrequest?view=netframework-4.8) to help me use the ftpwebrequest function and I saw in red that spelled this text: – Christopher Harkins Oct 04 '19 at 00:52
  • **Unless the EnableSsl property is true, all data and commands, including your user name and password information, are sent to the server in clear text. Anyone monitoring network traffic can view your credentials and use them to connect to the server. If you are connecting to an FTP server that requires credentials and supports Secure Sockets Layer (SSL), you should set EnableSsl to true.** – Christopher Harkins Oct 04 '19 at 00:53
  • So I would imagine to enable that It would look something like this `request.EnableSsl = true;` Is there a similar option to any of the solution functions you provided or is it all integrated? – Christopher Harkins Oct 04 '19 at 00:55
  • My answer is still the same: *"Your comment/question makes little sense to me. **Encryption is FTP protocol thing, not related to specific commands.** Consider asking a separate question if this is not clear to you and you need a full answer."* – Martin Prikryl Oct 04 '19 at 05:41