File Size via FTP

Is there a preferred way to obtain the size of a file stored on an FTP server using the 2.0 Framework?

The best I've come up with is to contact the FTP server via an FtpWebRequest object, with its .Method property set to WebRequestMethods.Ftp.GetFileSize.

This causes the size to be returned in the FtpWebResponse object's StatusDescription property. But its preceded by the Status Code value, so I have to parse the StatusDescription to obtain the file size.

That's no big deal. But oddly, the FtpWebResponse object has properties that return the parsed value of replies such as the file's date of last modification. But it does not, as far as I can tell, handle file sizes.

I've looked at other ways to connect to FTP sites, such as the WebClient class, and My.Computer.Network. But neither of these seem to offer anything better.

Suggestions?

Karen

[950 byte] By [KarenKenworthy] at [2008-2-22]
# 1
I just perform a directory listing in order to get file sizes.

My two cents,

Richard Rosenheim

RichardR at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 2
response.ContentLength should give you the size
Malar at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 3
<< I just perform a directory listing in order to get file sizes. >>

Thanks for the reply!

In my case, I'm only looking for the file size of a single file. So having to parse the reply to a .ListDirectoryDetails request is more work than I'd like.

Karen

KarenKenworthy at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 4
<< response.ContentLength should give you the size >>

Thanks! That sounds like the way to go.

Karen

KarenKenworthy at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 5
<< response.ContentLength should give you the size >>

For some reason, I thought .ContentLength gave the length of the response, not the length of the file. Now I know why:

From help's Remarks on the FtpWebResponse class:

"Any data returned by the request, such as the list of file names returned for a ListDirectory request, is available in the stream returned by the GetResponseStream method. The length of the stream data can be obtained from the ContentLength property."

And from the list of FtpWebResponse Members:

"ContentLength Gets the length of the data received from the FTP server."

Hope that gets clarified before the final release.

Karen

KarenKenworthy at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 6
One other suggestion for the Framework team -- please synchronize the names of the FTP methods and properties.

For example, to retrieve the date of last modification of a file on an FTP server, the FtpWebRequest's method property must be set to:

WebRequestMethods.Ftp.GetDateTimestamp


But the returned value is stored in the FtpWebResponse object's property named:

.LastModified

Likewise, to retrieve a file's size, you use this value for the FtpWebRequest object's Method property:

WebRequestMethods.Ftp.GetFileSize

while the corresponding property in the response object is:

ContentLength

What's more, the ContentLength property appears to be overloaded (can you overload a property?). It can mean the length of the stream returned in response to a request. But in the special case of the .GetFileSize method, it returns the size of the remote file, not the length of the FTP server's response. :(

How about a .FileSize response property? And a .DateTimestamp property? Or a .GetLastModified method value?

Karen

KarenKenworthy at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 7
Thanks for your suggestion. I have passed this request on to our PM and will evaluate the suggestion for future releases/versions.
mahjayar at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 8
mahjayar wrote:
Thanks for your suggestion. I have passed this request on to our PM and will evaluate the suggestion for future releases/versions.

Thanks!

I really appreciate the addition of FTP support. It just needs a couple of rough edges filed off. :)

Karen

KarenKenworthy at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...

.NET Development

Site Classified