【问题标题】:Dir exists or not on FTP目录在 FTP 上是否存在
【发布时间】:2011-01-01 13:49:05
【问题描述】:

如何检查服务器上是否存在某些 DIR? 虽然我可以通过以下方式检查文件是否存在:

try
{
    FtpWebRequest request=null;

    request = (FtpWebRequest)WebRequest.Create("ftp://" + webrequestUrl + "/somefile.txt");
    request.Credentials = new NetworkCredential(username, password);
    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)
        {
            //task
        }
    }
}

但是如何检查 DIR?如果我只在 URI 中指定 DIR,那么如果 DIR 不存在,它就不会去捕获。

【问题讨论】:

  • 令人印象深刻的 11 个问题,而您从未接受过答案...
  • 虽然在我没有得到任何确切答案之前,我需要他们来操作它们,所以我没有将它们标记为答案,因为其他用户(正在搜索某些查询)会从正确答案转移.
  • @marshalprince 那位先生,没有任何意义。
  • 接受答案是感谢用户花时间帮助您的一种方式。如果您没有得到任何确切的答案,可能是因为您的问题不够详细

标签: c# ftp ftp-client


【解决方案1】:
request = (FtpWebRequest)WebRequest.Create("ftp://" + webrequestUrl); //no file name
request.Credentials = new NetworkCredential(username, password);
myFtpRequest.Method = WebRequestMethods.Ftp.ListDirectory;

并检查您的文件/目录是否已列出。

您需要询问响应,它应该包含可能的文件和目录的列表。

您不应该使用 catch 来处理程序流。 MSDN example

【讨论】:

    【解决方案2】:

    我不认为代码做你认为它做的事。 据我了解您正在尝试为文件获取ls 的文档(想想DOS/Windows 中的dir,目录中的文件列表)。那没有意义。它在某种程度上有效,因为您在尝试访问 目录 "somefile.txt" 时遇到了异常。

    通过查看父级的 ListDirectory 响应的输出,您应该能够以正确的方式 (tm) 执行此操作:

    做一个 ListDirectory ftp://yourserver/ 并检查是否

    • 您的文件
    • 您的目录

    已列出。

    【讨论】:

    • 那么如果我需要去一些目录,那么为什么我在代码中没有发现任何异常。像someip.com/folder/folder 这样,如果文件夹目录中没有文件夹,则不会返回任何异常。
    • 好的。这是一种方法,但它会在代码中产生额外的开销。为什么响应不会出现为 null 或抛出异常(如果不存在)。
    【解决方案3】:

    我用:

    private bool CreateFTPDirectory(string directory)
    {
    
        try
        {
            //create the directory
            FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpURI+"/"+directory));
            requestDir.Method = WebRequestMethods.Ftp.MakeDirectory;
            requestDir.UsePassive = true;
            requestDir.UseBinary = true;
            requestDir.KeepAlive = false;
            //requestDir.UseDefaultCredentials = true;
            requestDir.Credentials = new NetworkCredential(UserId, Password);
            requestDir.Proxy = WebRequest.DefaultWebProxy;
            requestDir.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
    
            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) || (((int)response.StatusCode)==521))
            {
                response.Close();
                return true;
            }
            else
            {
                response.Close();
                return false;
            }
        }
    }
    

    这也有创建目录的副作用。如果它已经存在,则会返回 521 结果,该结果未在 .NET 枚举中定义。

    当您连接到 FTP 服务器时,您可以将 Uri 指定为“ftp//ftp.domain.com/somedirectory”,但这会转换为:“ftp://ftp.domain.com/homedirectoryforftp/somedirectory”。为了能够定义完整的根目录,请使用“ftp://ftp.domain.com//somedirectory”,它转换为机器上的 //somedirectory。

    【讨论】:

      【解决方案4】:

      通过使用edtFTPnet

        private void ftp_folder_IsExists()
      {
          FTPClient ftp = default(FTPClient);
      
          ftp = new FTPClient("ftp_host_name_here");  // ex.:- no need to use ftp in the host name, provide name only
          ftp.Login("username", "password");
      
          string[] file_and_folders = ftp.Dir(".", false);// . is used to get all the [files and folders] in the root of FTP
      
          string[] file_and_folders_1 = ftp.Dir("MyFolder", false);// this will get all the [files and folder] inside MyFolder (ex. ftp.ahostname.com/MyFolder/)
      
      
          //checking for a FILE
          if (file_and_folders.Contains("something.txt")) {
              //Do what you want..
          } else {
              //Do what you want..
      
          }
      
          //checking for a FOLDER
          if (file_and_folders.Contains("A_Folder")) {
              //Do what you want..
          } else {
              //Do what you want..
      
          }
      
      }
      

      注意:用 VB.NET 编写并使用 http://converter.telerik.com/ 转换的代码

      【讨论】:

        猜你喜欢
        • 2020-06-07
        • 2013-01-16
        • 1970-01-01
        • 2013-05-19
        • 2011-02-15
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        相关资源
        最近更新 更多