zoukankan      html  css  js  c++  java
  • ftp访问空目录的返回

    protected override bool DirectoryExists(string path)
            {
                bool exists = false;
                if (path.LastIndexOf('/') != path.Length - 1)
                {
                    path = path + '/';
                }
                FtpWebResponse response = null;
                FtpWebRequest request = null;
                int retryCount = 0;
                while (true)
                {
                    try
                    {
                        request = CreateFtpRequest("ftp://" + HostIP + path);
                        request.Method = WebRequestMethods.Ftp.ListDirectory;
                        response = (FtpWebResponse)request.GetResponse();
                        using (TextReader tr = new StreamReader(response.GetResponseStream()))
                        {
                            string text = tr.ReadToEnd();
                            exists = true;
                        }
                        break;
                    }
                    catch (WebException e)
                    {
                        if (response != null && response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                        {
                            exists = false;
                            break;
                        }
                        else
                        {
                            response = e.Response as FtpWebResponse;
                            if (response != null && response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                            {
                                exists = false;
                                break;
                            }
                        }
                        
                        if (HandleWebException(e, ++retryCount))
                        {
                            continue;
                        }
                    }
                    finally
                    {
                        if (response != null)
                        {
                            response.Close();
                            response = null;
                        }
                    }
                }
    
                return exists;
            }

    这个方法是用来判断ftp路径是否存在,一开始并没有去判断path变量末尾有没有"/",后来发现,如果末尾没有"/",即使访问了一个空目录,也不会抛出异常,也就是说会返回一个550,但是不会抛出异常。

  • 相关阅读:
    新能源汽车三大核心技术
    新能源汽车分类解读
    hadoop(四):配置参数
    HDP2.4安装(六):小结与回顾
    【Web】Nginx配置开机启动
    【Linux】ODBC安装
    【Erlang】源码安装
    【Linux】 Ncures库的介绍与安装
    【RabbitMQ】 RabbitMQ安装
    《MinDoc 接口文档在线管理系统》
  • 原文地址:https://www.cnblogs.com/middlesummer/p/3555306.html
Copyright © 2011-2022 走看看