[RESOLVED] [2.0][2005] FTPWebrequest RemoveDirectory
Hi all,
I have been working on an app, part of which connects to a FTP site. I have code working to listdirectories, download and upload files. What i now want to do is delete directories from the FTP server once i have finished with them. I have written this code which is based on the delete file code found on the MSDN site:
Code:
public bool DeleteDirectoryOnServer(Uri serverUri)
{
// The serverUri parameter should use the ftp:// scheme.
// It contains the name of the server file that is to be deleted.
// Example: ftp://contoso.com/someFile.txt.
//
try
{
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return false;
}
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.RemoveDirectory;
request.Credentials = m_networkcred;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Debug.Print(response.StatusCode.ToString());
Debug.Print("Delete status: {0}", response.StatusDescription);
response.Close();
return true;
}
catch (Exception ex)
{
Debug.Print(ex.Message);
return false;
}
}
When running the code i get the following error:
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
Could anyone shed some light on why this could be happening? I know the directory is there. There is also very little information available on the RemoveDirectory webrequestmethod online.
All help is greatly appreciated.
Thanks,
Grant
RESOLUTION:
I found a resolution for the problem. It appears i can only remove empty directories. I'll need to loop through the contents and delete any files before removing the folder.
Re: [RESOLVED] [2.0][2005] FTPWebrequest RemoveDirectory
no. the problem is still there. i have an empty folder which raises this error.
some body hlp pls
Re: [RESOLVED] [2.0][2005] FTPWebrequest RemoveDirectory
here is my code even with loop iteration through folder contents.
public bool deleteFolderContents(string remoteFolder)
{
bool rslt = false;
try
{
string[] list = directoryListSimple(remoteFolder);
List<string> files = new List<string>();
List<string> dirs = new List<string>();
foreach (string item in list)
{
if (item.Contains('.'))
files.Add(item);
else
dirs.Add(item);
}
foreach (string file in files)
{
deleteFile(remoteFolder + "/" + Path.GetFileName(file));
}
foreach (string dir in dirs)
{
deleteFolderContents(remoteFolder + "/" + Path.GetFileName(dir));
deleteFolder(remoteFolder + "/" + Path.GetFileName(dir));
}
rslt = true;
}
catch (Exception)
{
rslt = false;
}
return rslt;
}
public void deleteFolder(string deleteFolder)
{
try
{
/* Create an FTP Request */
ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFolder);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
/* Resource Cleanup */
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return;
}