【问题标题】:Check if directory exists on FTP server检查FTP服务器上是否存在目录
【发布时间】:2013-01-16 03:44:15
【问题描述】:

我正在检查我的 FTP 服务器上是否存在目录:

    public bool DirectoryExists(string directory)
    {
        bool directoryExists;

        var request = (FtpWebRequest)WebRequest.Create(directory);
        request.Method = WebRequestMethods.Ftp.ListDirectory;
        request.Credentials = new NetworkCredential("user", "pass");

        try
        {
            using (request.GetResponse())
            {
                directoryExists = true;
            }
        }
        catch (WebException)
        {
            directoryExists = false;
        }

        return directoryExists;
    }

在这种情况下:

directory = @"ftp://ftp.example.com/Rubicon";

在我的服务器上,我有一个名为 Rubicon1 的文件夹。这导致我的支票返回true。除非它与目录名称完全匹配,否则如何确保它失败?

【问题讨论】:

  • 你能得到父目录的列表吗?也许它被隐藏了
  • 看这个:stackoverflow.com/questions/265953/… 推理适用于任何易失性资源,其中肯定包括 FTP 共享。
  • @ericosg 上面写着Create 的那一行是用于创建 FTP Web 请求,而不是实际的 FTP 目录
  • 刚刚用我的 FTP 试了一下,它按预期工作。会不会又是their fault? :)
  • @w0lf,谢谢,我知道这一点,但我的表述很糟糕。

标签: c# ftp ftpwebrequest


【解决方案1】:

我通过将目录更改为成功解决了这个问题:

directory = @"ftp://ftp.example.com/Rubicon/";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2013-05-19
    • 1970-01-01
    • 2011-11-16
    相关资源
    最近更新 更多