How to check if an FTP directory exists

前端 未结 10 1403
一个人的身影
一个人的身影 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 11:17

    I would try something along this lines:

    • Send MLST FTP command (defined in RFC3659) and parse it's output. It should return valid line with directory details for existing directories.

    • If MLST command is not available, try changing the working directory into the tested directory using a CWD command. Don't forget to determine the current path (PWD command) prior to changing to a tested directory to be able to go back.

    • On some servers combination of MDTM and SIZE command can be used for similar purpose, but the behavior is quite complex and out of scope of this post.

    This is basically what DirectoryExists method in the current version of our Rebex FTP component does. The following code shows how to use it:

    string path = "/path/to/directory";
    
    Rebex.Net.Ftp ftp = new Rebex.Net.Ftp();
    ftp.Connect("hostname");
    ftp.Login("username","password");
    
    Console.WriteLine(
      "Directory '{0}' exists: {1}", 
      path, 
      ftp.DirectoryExists(path)
    );
    
    ftp.Disconnect();
    

提交回复
热议问题