This project is read-only.

DirectoryExists method always False

Feb 8, 2011 at 1:23 AM

why DirectoryExists method always return false even if the directory exists indeed?

Oct 3, 2011 at 1:12 PM
Edited Oct 3, 2011 at 1:12 PM

I saw that the mothod return false when the directory is empty. Byt I don't know why!

Dec 7, 2011 at 4:47 PM
bugmenot2 wrote:

I saw that the mothod return false when the directory is empty. Byt I don't know why!

I confirm this behavior

Jan 11, 2012 at 6:50 PM

It's a bug of the ftp protocol or with the library?

In the second case, can the author fix this?


Jun 12, 2013 at 9:06 PM

I got the same issue. The reason is that the author used FtpFindFirstFile method for checking whether a directory exists or not; therefore; it always return false if the given directory is empty. See below
public bool DirectoryExists(string path)
            WINAPI.WIN32_FIND_DATA findData = new WINAPI.WIN32_FIND_DATA();

            IntPtr hFindFile = WININET.FtpFindFirstFile(
                ref findData,
                if (hFindFile == IntPtr.Zero)
                    return false;

                return true;
                if (hFindFile != IntPtr.Zero)

My suggestion is that we will use GetDirectories method as below:
               using (FtpConnection ftp = new FtpConnection(ftpHost, username, password))
                    ftp.Open(); /* Open the FTP connection */
                    ftp.Login(); /* Login using previously provided credentials */
                    /* Get parent directory of the given path */
                    String parrentDirectory = String.Format("/{0}",Path.GetDirectoryName(directory).Replace(Path.DirectorySeparatorChar,'/'));
                    /* Get name of the directory */
                    String checkingDirectory = String.Format("{0}", Path.GetFileName(directory)).ToLower();
                    /* Get all child directories info of the parent directory */
                    var ftpDirectories = ftp.GetDirectories(parrentDirectory);
                    /* check if the given directory exists in the returned result */
                    result = ftpDirectories.Any(d => d.Name.ToLower() == checkingDirectory);