C# Vérifie si le Répertoire FTP EXISTE

J'ai cette Méthode FTP qui vérifie si un répertoire existe. Il fonctionne très bien au début, mais maintenant, c'est encore de retourner true, même si le répertoire n'existe pas. J'ai essayé beaucoup de choses, et de définir un point d'arrêt pour voir ce que la propriété de l'objet de la réponse que je peux utiliser pour déterminer si le répertoire existe ou pas. J'ai aussi cherché sur internet et les solutions ne semblent pas fonctionner pour moi. Voici ma méthode FTP.

public bool directoryExists(string directory)
            {
                /* Create an FTP Request */
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
                /* Log in to the FTP Server with the User Name and Password Provided */
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                /* Specify the Type of FTP Request */
                ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                try
                {
                    using (ftpRequest.GetResponse())
                    {
                        return true;
                    }
                    //var response = ftpRequest.GetResponse();
                    //if (response != null)
                    //   return true;
                    //else return false;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    return false;
                }

                /* Resource Cleanup */
                finally
                {
                    ftpRequest = null;
                }
            }

Et voici la méthode qui l'appelle et renvoie true, même si le répertoire n'existe pas:

private string getDirectory(ref FtpClass ftp, string internalID)
        {
            string remoteSubPathDel = internalID + "\\trunk\\prod\\xml\\delete";
            string remoteSubPathUpdate = internalID + "\\trunk\\prod\\xml\\update";
            string remoteSubPathNew = internalID + "\\trunk\\prod\\xml\\new";
            if (ftp.directoryExists(remoteSubPathDel))
                return remoteSubPathDel;
            else if (ftp.directoryExists(remoteSubPathUpdate))
                return remoteSubPathUpdate;
            else if (ftp.directoryExists(remoteSubPathNew))
                return remoteSubPathNew;
            else
                return String.Empty;
        }

Espère que quelqu'un peut vous aider. Merci! 🙂

  • Vous allez courir avec FTP. Les informations retournées par un serveur FTP n'est pas définie par la RFC de sorte que certains clients seront pas en mesure d'analyser les réponses.
InformationsquelleAutor finnTheHumin | 2013-05-24