当有文件夹重复时,新那会出错,我如何判断这个文件夹是否存在呢,我知道一个是新那时添加异常处理,有没有其它好的办法呢?

解决方案 »

  1.   


            public bool CheckFileExist(string ftpFilePath)
            {
                FtpWebRequest ftpWebRequest = null;
                WebResponse webResponse = null;
                StreamReader reader = null;            try
                {
                    int s = ftpFilePath.LastIndexOf('/');
                    if (s == ftpFilePath.Length - 1)
                    {
                        ftpFilePath = ftpFilePath.Substring(0, ftpFilePath.Length - 1);
                        s = ftpFilePath.LastIndexOf('/');
                    }                string ftpFileName = ftpFilePath.Substring(s + 1, ftpFilePath.Length - s - 1);
                    string uri = GetFtpUri(ftpHostAddress, ftpPort, ftpFilePath.Substring(0, s + 1));                ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                    ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                    ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;
                    ftpWebRequest.UsePassive = false;
                    ftpWebRequest.KeepAlive = false;
                    webResponse = ftpWebRequest.GetResponse();
                    reader = new StreamReader(webResponse.GetResponseStream());
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        if (line == ftpFileName)
                        {
                            return true;
                        }
                        line = reader.ReadLine();
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                    if (webResponse != null)
                    {
                        webResponse.Close();
                    }
                }
                return false;
            }
      

  2.   

    WebRequestMethods.Ftp.ListDirectory
    获取FTP请求的响应流中的字符串
      

  3.   

    本来想用ListDirectory,然后分析一下
    后没时间研究
    我也用异常处理解决的期待更好的办法