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.
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.