Unable to Delete FTP Directory

Hello,

I am unable to delete directories from a FTP server. However I am able to delete from windows explorer/IE but not from my c# code. I always get the following error

System.Net.WebException: The remote server returned an error: (550) File unavailable

The following is the C# code I am using.

try

{

string URI = HostName + AdjustDir(dirpath);

FtpWebRequest ftp = GetRequest(URI);

ftp.Method =WebRequestMethods.Ftp.DeleteFile;

string str = GetStringResponse(ftp);

using (FtpWebResponse response =((FtpWebResponse)(ftp.GetResponse())))

{

long size = response.ContentLength;

using (Stream datastream = response.GetResponseStream())

{

using (StreamReader sr =newStreamReader(datastream))

{

result = sr.ReadToEnd();

sr.Close();

}

datastream.Close();

}

response.Close();

}

returntrue;

}

catch (Exception ex)

{

Utilities.GetExceptionDetails.ParseException(ex);

returnfalse;

}

Looking forward to some help.
[6084 byte] By [Mamidanna] at [2008-1-7]
# 1

I have worked with the FtpWebRequest and FtpWebResponse classes. And the error you mentioned is thrown when the path to that file on the Ftp server is wrong. Check out the path while debugging and see if it is correct. Try other Ftp operations using that same path and see if it works.
decyclone at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Hello Joshi,

I thank you for your time. I have verified that the supplied URL is correct. Another interresting fact is that I am able to delete the drectory if its is empty.

Awaiting your reply,

Warm Regards,

Krishna Murthy

Mamidanna at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
Mamidanna wrote:

Hello Joshi,

I thank you for your time. I have verified that the supplied URL is correct. Another interresting fact is that I am able to delete the drectory if its is empty.

Awaiting your reply,

Warm Regards,

Krishna Murthy

Hi Krishna Murthy,

That's what the problem is. I forgot it completely. You can not delete a directory/folder if it is not empty on Ftp server. You must delete all the files and folders in it before you can delete it. It's just like old DOS days when using RMDIR caused this kind of error.

In my project I was talking about in my previous post, I didn't have to remove directories but only create them and add/delete files in them. So, never faced this problem, but try looping through the folder's contents and delete all the files and folders in the folder and then, delete it. Recursion should save you from this mess.

Another point, research on FtpClient class. I never used it, but I have heard it provides some Ftp functionalities. May be that can help.

decyclone at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Hello Joshi,

Thanks a lot, I shall try deleting all the files before deleting the directory

Warm Regards,

Krishna Murthy Mamidanna

Mamidanna at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic Language...