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,但是不会抛出异常。

  • 相关阅读:
    MVC新手指南
    BufferedReader方法-----Scanner方法
    sin=in.readLine();
    STL:string 大小(Size)和容量(Capacity)
    2014=9=24 连接数据库2
    2014=9=24 连接数据库1
    常用英语单词
    Linux权限详解(chmod、600、644、666、700、711、755、777、4755、6755、7755)
    linux 常用快捷键
    启动sh文件注意的问题
  • 原文地址:https://www.cnblogs.com/middlesummer/p/3555306.html
Copyright © 2011-2022 走看看