FTP下载文件,每五分钟查询一次,直到下载完毕

  bool isStop = false;
               while (!isStop)
                { 
                
                    isStop = FTPManager.DownloadFile(fileName);
                    // Set the Interval to 5 minutes.
                    System.Threading.Thread.Sleep(300000);
                }                 

            }



public class FTPManager
    {    
        public static bool DownloadFile(string fileName)
        {
            bool isStop = false;
            try
            {               
                
                LogManager.Instance.LogInformation("Connecting To FTP Server...");


                FtpWebRequest request = WebRequest.Create(string.Format("{0}/{1}",
                    Configuration.FTPServer, fileName)) as FtpWebRequest;
                request.Method = "RETR";
                request.Credentials = new NetworkCredential(Configuration.FTPUser, Configuration.FTPPassword);
                request.UsePassive = true;
                request.UseBinary = true;
                request.KeepAlive = false;


                using (WebResponse response = request.GetResponse() as FtpWebResponse)
                {
                    LogManager.Instance.LogInformation(string.Format("Downloading File {0}... ", fileName));


                    using (MemoryStream memStream = new MemoryStream())
                    {
                        using (Stream responseStream = response.GetResponseStream())
                        {
                            byte[] buffer = new byte[0x400];
                            int readBytes = 0;
                            while ((readBytes = responseStream.Read(buffer, 0, buffer.Length)) != 0)
                                memStream.Write(buffer, 0, readBytes);
                        }


                        using (FileStream fileStream = new FileStream(string.Format(@"{0}\{1}", Configuration.DownloadDirectory, fileName), FileMode.Create))
                        {
                            memStream.WriteTo(fileStream);
                            isStop = true;
                        }


                        LogManager.Instance.LogInformation(string.Format("File {0} Downloaded Successfully.", fileName));
                    }
                }
                //LogManager.Dispose();
            }
            catch (WebException wex)
            {
                FtpWebResponse response = wex.Response as FtpWebResponse;
                if (response != null)
                {
                    if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)//判断FTP上是否存在该文件
                        LogManager.Instance.LogInformation(string.Format("File '{0}' Not Found On FTP.", fileName));
                    else
                        LogManager.Instance.LogException(wex);
                }
            }
            catch (Exception exception)
            {
                LogManager.Instance.LogException(exception);
            }
            return isStop;
        }


    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值