views:

374

answers:

3

I'm trying to use an FtpWebRequest to get the size of a file on a company FTP. Yet whenever I try to get the response an exception is thrown. See the error details in the catch block in the code below.

string uri = "ftp://ftp.domain.com/folder/folder/file.xxx";
FtpWebRequest sizeReq = (FtpWebRequest)WebRequest.Create(uri);
sizeReq.Method = WebRequestMethods.Ftp.GetFileSize;
sizeReq.Credentials = cred;
sizeReq.UsePassive = proj.ServerConfig.UsePassive; //true
sizeReq.UseBinary = proj.ServerConfig.UseBinary; //true
sizeReq.KeepAlive = proj.ServerConfig.KeepAlive; //false

long size;

try
{
//Exception thrown here when I try to get the response
using (FtpWebResponse fileSizeResponse = (FtpWebResponse)sizeReq.GetResponse())
{
size = fileSizeResponse.ContentLength;
}
}
catch(WebException exp)
{
FtpWebResponse resp = (FtpWebResponse)exp.Response;
MessageBox.Show(exp.Message); // "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."
MessageBox.Show(exp.Status.ToString()); //ProtcolError
MessageBox.Show(resp.StatusCode.ToString()); // ActionNotTakenFileUnavailable
MessageBox.Show(resp.StatusDescription.ToString()); //"550 SIZE: Operation not permitted\r\n"
}

This code does work, however, when connected to my personal FTP. The StatusDescription of the response says that the operation is "not permitted". Could it be that my office FTP just wont allow for the querying of a file size?

I've also tried listing the directory details, which will return the size, and have noticed that my office FTP reports the directory details in a different format then my personal FTP. Maybe this is the problem?

//work ftp ListDirectoryDetails
-rw-r--r--   1 (?)      user    12345 Nov 16 20:28 some file name.xxx

//personal ftp ListDirectoryDetails
-rw-r--r--    1 user user 12345 Mar 13  some file name.xxx

From reading this blog post I think that my personal ftp is returning a Unix formatted response, but my work is returning a windows formatted response. Maybe this is unrelated but I thought I'd mention it.

A: 

Looks like on the work FTP that you have no owner account specified therefore getting denied.

Big_E
A: 

It occurred to me after the login was processed, and got a default user directory as current directory. I was specifying the complete path as uri, including my user directory. I traced it down with WireShark, you might find the same result.

Robert den Hartog
+1  A: 

I got problems because of basic naming of files: When you connect to an FTP server you might specify the Uri as "ftp//ftp.domain.com/somedirectory" but this translates to: "ftp://ftp.domain.com/homedirectoryforftp/somedirectory". To be able to define the full root directory use "ftp://ftp.domain.com//somedirectory" which translates to //somedirectory on the machine.

Tony Lambert