How to check if an FTP directory exists

前端 未结 10 1393
一个人的身影
一个人的身影 2020-12-01 10:26

Looking for the best way to check for a given directory via FTP.

Currently i have the following code:

private bool FtpDirectoryExists(string direct         


        
10条回答
  •  旧巷少年郎
    2020-12-01 10:59

    User this code it may be your answer..

     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://mydomain.com/abcdir", txtUsername.Text, txtPassword.Text);
    

    Why use another library - create your own logic's.

提交回复
热议问题