Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an FTP directory exists

Looking for the best way to check for a given directory via FTP.

Currently i have the following code:

private bool FtpDirectoryExists(string directory, string username, string password) {      try     {         var request = (FtpWebRequest)WebRequest.Create(directory);         request.Credentials = new NetworkCredential(username, password);         request.Method = WebRequestMethods.Ftp.GetDateTimestamp;          FtpWebResponse response = (FtpWebResponse)request.GetResponse();     }     catch (WebException ex)     {         FtpWebResponse response = (FtpWebResponse)ex.Response;         if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)             return false;         else             return true;     }     return true; } 

This returns false whether the directory is there or not. Can someone point me in the right direction.

like image 938
Billy Logan Avatar asked May 04 '10 21:05

Billy Logan


People also ask

How do I find an FTP directory?

You can attempt to list the directory and check for an error StatusCode. try { FtpWebRequest request = (FtpWebRequest)WebRequest. Create("ftp://ftp.microsoft.com/12345"); request. Method = WebRequestMethods.

What is FTP default directory?

The default vsftpd login directory for a normal user is the home directory of the system normal user; and the default vsftpd login directory for the anonymous user is /var/ftp .


2 Answers

Basically trapped the error that i receive when creating the directory like so.

private bool CreateFTPDirectory(string directory) {      try     {         //create the directory         FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(directory));         requestDir.Method = WebRequestMethods.Ftp.MakeDirectory;         requestDir.Credentials = new NetworkCredential("username", "password");         requestDir.UsePassive = true;         requestDir.UseBinary = true;         requestDir.KeepAlive = false;         FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse();         Stream ftpStream = response.GetResponseStream();          ftpStream.Close();         response.Close();          return true;     }     catch (WebException ex)     {         FtpWebResponse response = (FtpWebResponse)ex.Response;         if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)         {             response.Close();             return true;         }         else         {             response.Close();             return false;         }       } } 
like image 136
Billy Logan Avatar answered Sep 18 '22 10:09

Billy Logan


I was also stuck with a similar problem. I was using,

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftpserver.com/rootdir/test_if_exist_directory");   request.Method = WebRequestMethods.Ftp.ListDirectory;   FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 

and waited for an exception in case the directory didn't exist. This method didn't throw an exception.

After a few hit and trials, I changed the directory from: "ftp://ftpserver.com/rootdir/test_if_exist_directory" to: "ftp://ftpserver.com/rootdir/test_if_exist_directory/". Now the code is working for me.

I think we should append forwardslash (/) to the URI of the ftp folder to get it to work.

As requested, the complete solution will now be:

public bool DoesFtpDirectoryExist(string dirPath) {     try     {         FtpWebRequest request = (FtpWebRequest)WebRequest.Create(dirPath);           request.Method = WebRequestMethods.Ftp.ListDirectory;           FtpWebResponse response = (FtpWebResponse)request.GetResponse();         return true;      }      catch(WebException ex)      {          return false;      } }  //Calling the method (note the forwardslash at the end of the path): string ftpDirectory = "ftp://ftpserver.com/rootdir/test_if_exist_directory/"; bool dirExists = DoesFtpDirectoryExist(ftpDirectory); 
like image 30
Bikash Avatar answered Sep 21 '22 10:09

Bikash