Wednesday, July 20, 2011

check directory Exists in Ftp Server

 

public bool FtpDirectoryExists(string directoryPath, string ftpUser, string ftpPassword)

        {

            bool IsExists = true;

            try

            {

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(directoryPath);

                request.Credentials = new NetworkCredential(ftpUser, ftpPassword);

                request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;

 

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            }

            catch (WebException ex)

            {

                IsExists = false;

            }

            return IsExists;

        }

 

I have called this method as:
bool result = FtpActions.Default.FtpDirectoryExists( "ftp://domain.com/test",
                                                    txtUsername.Text, txtPassword.Text);

2 comments :

  1. thanks....
    i found here...
    you're save my time dud!!!!

    ReplyDelete
  2. I was using a similar approach, but now it's no longer working. FtpWebRequest.GetResponse() no longer seems to throw an exception when called for a directory that doesn't exist. The only change I can think of since it worked is that I've upgraded to Visual Studio 2013.

    ReplyDelete