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.
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.
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 .
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; } } }
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With