Example #1
0
        /// <summary>
        /// GET
        /// </summary>
        /// <param name="subDir">サブディレクトリ</param>
        /// <param name="fileName">ファイ名</param>
        /// <param name="localPath">ローカルパス(フルパス)</param>
        /// <returns>T:成功 F:失敗</returns>
        public bool Get(string subDir, string fileName, string localPath)
        {
            if (this.Log != null)
            {
                this.Log.Start();
            }

            string url = StrUtil.AppendUrl("ftp://" + this.Host, this.DefPath);

            url = StrUtil.AppendUrl(url, subDir);
            url = StrUtil.AppendUrl(url, fileName);

            try
            {
                if (this.Log != null)
                {
                    this.Log.Info("FTP Get [{0}]", url);
                }

                using (var client = new WebClient())
                {
                    client.Credentials = this.Credential;
                    client.DownloadFile(url, localPath);
                }

                if (this.Log != null)
                {
                    this.Log.Info("ファイルGET[{0}]→[{1}]", url, localPath);
                }
                return(true);
            }
            catch (Exception ex)
            {
                if (this.Log != null)
                {
                    this.Log.Error("ファイルGETでエラー発生:" + ex);
                }
                return(false);
            }
            finally
            {
                if (this.Log != null)
                {
                    this.Log.End();
                }
            }
        }
Example #2
0
        /// <summary>
        /// ディレクトリ内のファイル一覧取得
        /// </summary>
        /// <param name="subDir">サブディレクトリ</param>
        /// <param name="list">ファイル名のリスト</param>
        /// <returns>T:成功 F:失敗</returns>
        /// <remarks>ファイルとディレクトリの区別はできない
        /// 区別が必要な時、ListDirectoryDetailsを使用
        /// </remarks>
        public bool ListDir(string subDir, List <string> list)
        {
            if (this.Log != null)
            {
                this.Log.Start();
            }

            string url = StrUtil.AppendUrl("ftp://" + this.Host, this.DefPath);

            url = StrUtil.AppendUrl(url, subDir);
            var request = (FtpWebRequest)WebRequest.Create(url);

            request.Credentials = this.Credential;
            //request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            request.Method     = WebRequestMethods.Ftp.ListDirectory;
            request.KeepAlive  = true;                  //KeepAlive = ON (WebClientに合わせる)
            request.UseBinary  = true;
            request.UsePassive = false;

            try
            {
                if (this.Log != null)
                {
                    this.Log.Info("FTP ListDirectory [{0}]", url);
                }

                using (var response = (FtpWebResponse)request.GetResponse())
                {
                    using (var stream = new StreamReader(response.GetResponseStream()))
                    {
                        while (stream.Peek() >= 0)
                        {
                            //var info = new EntryInfo(stream.ReadLine());
                            string file = stream.ReadLine();

                            //ファイル前のパスを削除(Linux用)
                            if (file.Contains("/") == true)
                            {
                                var split = file.Split('/');
                                file = split[split.Length - 1];
                            }

                            list.Add(file);
                        }
                    }
                }

                if (this.Log != null)
                {
                    this.Log.Info("ディレクトリ[{0}] ファイル件数[{1}]", url, list.Count);
                }

                return(true);
            }
            catch (Exception ex)
            {
                if (this.Log != null)
                {
                    this.Log.Error("ファイル一覧取得でエラー発生:" + ex);
                }
                return(false);
            }
            finally
            {
                if (this.Log != null)
                {
                    this.Log.End();
                }
            }
        }