views:

256

answers:

4

Hello.

I am using C# to upload some file to a ftp server. If the file already existed the FtpWebRequest timed out, so I thought to deleate it first.

However the WebRequestMethods.Ftp.DeleteFile also always times out. Am I doing something wrong?

Here is my code:

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(address);

request.Credentials = new NetworkCredential(Username, Password);
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.DeleteFile;

try
{
    FtpWebResponse resp = (FtpWebResponse)request.GetResponse();
}
catch (Exception e)
{
    ...         
}

EDIT: Oh and it doesn't matter witch file I am trying to delete. As long as the file exists the request will always time out. If the file doesn't exist a different exception is thrown.

Nothing is wrong with credentials, I can do other operations (upload/download without a problem). Also it's not a server problem, if I connect to it with a client (FileZilla) with the same username / pass everything works as it should.

Thank you for your help.

A: 

could be a lock on the file? can you delete it manually with another client?

PeanutPower
Yes if I connect to the ftp server with a client (filezila) I can delete it without a problem.
Rekreativc
+2  A: 

The thing I have found using this Ftp via FtpWebRequest, is it is inherently a lot slower (since it is using the HTTP protocol over port 80), and it drives me crazy because FileZilla can do it a lot quicker (obviously using FTP protocol over port 20/21). There is a open source ftp component found here, I do not know if it will work for you, but worth a shot.

I know this is a subjective answer that will get downvoted, but personally, using ftp over port 80 is bound to be a lot slower especially on file operations like what you are trying to achieve.

Hope this helps, Best regards, Tom.

tommieb75
I gave up and decided to go with the library you suggested. I am still curious to why this was happening in the first place, however I believe this just may be "one of them things"...
Rekreativc
+1  A: 

Do you have access to the logs of the FTP server? If you do have a look at what commands the FTPWebRequest is making. It could be that it is trying to list the directory before deleting it.

Another issue maybe that the server is in passive mode, I believe FileZilla may automagicly detect this, check the connection in filezilla to see.

Andrew Cox
Active / Passive mode is not an issue, just tried it and fails the same way for both options.
Rekreativc
how about whacking the timeout up to something large. e.g. request.Timeout = (60000 * 1) // 60000 * No of minutes
Andrew Cox
A: 

Knowing what commands are sent between client and FTP server could help find out what is causing the timeout. Would it be possible to use a packet analyzer such as Ethereal to capture the communication log?

Alternative approach could be using a third party FTP component and enabling logging in it. Following code uses our Rebex FTP:

// create client 
Ftp client = new Ftp();
// enable logging 
client.LogWriter = new Rebex.FileLogWriter(@"c:\temp\log.txt", Rebex.LogLevel.Debug); 

// connect
client.Connect("ftp.example.org");
client.Login("username", "password");

// browse directories, transfer files 
client.DeleteFile("file.txt");

// disconnect 
client.Disconnect();
Martin Vobr