Example #1
0
        /// <summary>
        /// 取得指定目录在Ftp服务器上得状态.
        /// </summary>
        /// <returns>目录在服务器上得状态.</returns>
        /// <param name="iParentUrl">上一层URL.</param>
        /// <param name="iTargetDir">目标目录.</param>
        /// <param name="iAccountId">账户.</param>
        /// <param name="iPwd">密码.</param>
        private TDirState GetDirStateOnFtpServer(
            string iParentUrl, string iTargetDir,
            string iAccountId, string iPwd)
        {
            FtpWebRequest  ftpRequest = null;
            FtpWebResponse response   = null;
            StreamReader   reader     = null;
            TDirState      state      = TDirState.None;

            try
            {
                Uri targetURI = new Uri(iParentUrl);
                ftpRequest             = (FtpWebRequest)FtpWebRequest.Create(targetURI);
                ftpRequest.Credentials = new NetworkCredential(iAccountId, iPwd);
                ftpRequest.KeepAlive   = false;
                ftpRequest.Method      = WebRequestMethods.Ftp.ListDirectory;
                ftpRequest.UseBinary   = true;
                response = ftpRequest.GetResponse() as FtpWebResponse;

                reader = new StreamReader(
                    response.GetResponseStream(), System.Text.Encoding.Default);
                string line = reader.ReadLine();
                state = TDirState.NotExist;
                // 循环遍历内容
                while (line != null)
                {
                    if (line.Equals(iTargetDir) == true)
                    {
                        state = TDirState.Exist;
                        break;
                    }
                    line = reader.ReadLine();
                }

                if (TDirState.NotExist == state)
                {
                    UtilsLog.Info("GetDirStateOnFtpServer", "State:{0} ParentUrl:{1} TargetDir:{2}",
                                  state.ToString(), iParentUrl, iTargetDir);
                }
            }
            catch (WebException exp)
            {
                UtilsLog.Exception("GetDirStateOnFtpServer", "ParentUrl:{0} TargetDir:{1} \n WebException: \n {2}",
                                   iParentUrl, iTargetDir, exp.Message);
                state = TDirState.Exception;
            }
            catch (IOException exp)
            {
                UtilsLog.Exception("GetDirStateOnFtpServer", "ParentUrl:{0} TargetDir:{1} \n IOException: \n {2}",
                                   iParentUrl, iTargetDir, exp.Message);
                state = TDirState.Exception;
            }
            catch (Exception exp)
            {
                UtilsLog.Exception("GetDirStateOnFtpServer", "ParentUrl:{0} TargetDir:{1} \n Exception: \n {2}",
                                   iParentUrl, iTargetDir, exp.Message);
                state = TDirState.Exception;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
            }

            return(state);
        }