C# How to check if an FTP Directory Exists

Posted by Billy Logan on Stack Overflow See other posts from Stack Overflow or by Billy Logan
Published on 2010-05-04T21:35:11Z Indexed on 2010/05/04 21:38 UTC
Read the original article Hit count: 869

Filed under:
|

Hello Everyone,

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.

Thanks in advance, Billy

© Stack Overflow or respective owner

Related posts about c#

Related posts about ftpwebrequest