如何检查FTP目录是否存在

Bil*_*gan 29 c# ftp ftpwebrequest

寻找通过FTP检查给定目录的最佳方法.

目前我有以下代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

无论目录是否存在,都返回false.有人能指出我正确的方向.

Bil*_*gan 17

基本上困住了我在创建目录时收到的错误.

private bool CreateFTPDirectory(string directory) {

    try
    {
        //create the directory
        FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(directory));
        requestDir.Method = WebRequestMethods.Ftp.MakeDirectory;
        requestDir.Credentials = new NetworkCredential("username", "password");
        requestDir.UsePassive = true;
        requestDir.UseBinary = true;
        requestDir.KeepAlive = false;
        FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse();
        Stream ftpStream = response.GetResponseStream();

        ftpStream.Close();
        response.Close();

        return true;
    }
    catch (WebException ex)
    {
        FtpWebResponse response = (FtpWebResponse)ex.Response;
        if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
        {
            response.Close();
            return true;
        }
        else
        {
            response.Close();
            return false;
        }  
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 此代码不可靠:例如,如果您没有写权限且没有所需目录,则此函数将返回true. (13认同)
  • 还可以选择检查 FtpWebResponse 的 StatusDescription 属性。如果它包含“存在”(550 目录已存在)那么它已经存在。**然而** 我还没有找到任何规范或保证所有 FTP 服务器都必须返回该信息,FileZilla 可能就是这种情况。因此,请在您的特定场景中对其进行测试,并决定这是否是您想要做的事情/冒险的事情。 (2认同)

Bik*_*ash 9

我也遇到了类似的问题.我在用,

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftpserver.com/rootdir/test_if_exist_directory");  
request.Method = WebRequestMethods.Ftp.ListDirectory;  
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Run Code Online (Sandbox Code Playgroud)

并且在目录不存在的情况下等待异常.这种方法没有抛出异常.

经过几次点击和试验,我将目录从" ftp://ftpserver.com/rootdir/test_if_exist_directory "更改为:" ftp://ftpserver.com/rootdir/test_if_exist_directory/ ".现在这件作品对我有用.

我认为我们应该在ftp文件夹的uri中添加反斜杠(/)以使其工作.

根据要求,完整的解决方案现在将是:

public bool DoesFtpDirectoryExist(string dirPath)
{
    try
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(dirPath);  
        request.Method = WebRequestMethods.Ftp.ListDirectory;  
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        return true;
     }
     catch(WebException ex)
     {
         return false;
     }
}

//Calling the method:
string ftpDirectory = "ftp://ftpserver.com/rootdir/test_if_exist_directory/"; //Note: backslash at the last position of the path.
bool dirExists = DoesFtpDirectoryExist(ftpDirectory);
Run Code Online (Sandbox Code Playgroud)


Mah*_*hdi 8

我假设您已经熟悉FtpWebRequest,因为这是在.NET中访问FTP的常用方法.

您可以尝试列出目录并检查错误StatusCode.

    try 
{  
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.microsoft.com/12345");  
    request.Method = WebRequestMethods.Ftp.ListDirectory;  
    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())  
    {  
        // Okay.  
    }  
}  
catch (WebException ex)  
{  
    if (ex.Response != null)  
    {  
        FtpWebResponse response = (FtpWebResponse)ex.Response;  
        if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)  
        {  
            // Directory not found.  
        }  
    }  
} 
Run Code Online (Sandbox Code Playgroud)


Mar*_*obr 6

我会尝试这样的方法:

  • 发送MLST <directory> FTP命令(在RFC3659中定义)并解析它的输出.它应返回包含现有目录的目录详细信息的有效行.

  • 如果MLST命令不可用,请尝试使用CWD命令将工作目录更改为测试目录.在更改为测试目录之前,不要忘记确定当前路径(PWD命令)以便能够返回.

  • 在某些服务器上,MDTM和SIZE命令的组合可用于类似目的,但行为非常复杂,超出了本文的范围.

这基本上是当前版本的Rebex FTP组件中的DirectoryExists方法.以下代码显示了如何使用它:

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();
Run Code Online (Sandbox Code Playgroud)

  • 尽管其他答案提供了代码,但它们本质上是在创建一个新目录以查看是否发生错误。如果目录不存在,只需发出 FTP 'CWD' 命令,服务器将向该命令发出 5xx 回复代码。 (3认同)