基本信息
源码名称:C# ftp 下载类源码
源码大小:0.02M
文件格式:.rar
开发语言:C#
更新时间:2013-11-28
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace LibDldFile
{
    public class FtpDldFile : IDldFile
    {
        private string _ftpLoginUser = string.Empty;
        private string _ftpLoginPwd = string.Empty;
        private string _dldFileSaveDir = string.Empty;
        private List<string> _dldFileList = null;
        private UInt32 _ftpDldSize = 1024;

        /// <summary>
        /// 创建FTP 下载对象(文件保存路径默认为当前程序启动路径)
        /// </summary>
        /// <param name="dldFileList">下载文件路径列表</param>
        /// <param name="ftpLoginUser">FTP 登录用户</param>
        /// <param name="ftpLoginPwd">FTP 登录密码</param>
        public FtpDldFile(List<string> dldFileList, string ftpLoginUser, string ftpLoginPwd)
        {
            _dldFileList = dldFileList;
            _ftpLoginPwd = ftpLoginPwd;
            _ftpLoginUser = ftpLoginUser;
        }

        /// <summary>
        /// 创建FTP 下载对象
        /// </summary>
        /// <param name="dldFileList">下载文件路径列表</param>
        /// <param name="ftpLoginUser">FTP 登录用户</param>
        /// <param name="ftpLoginPwd">FTP 登录密码</param>
        /// <param name="dldFileSaveDir">下载文件保存路径</param>
        public FtpDldFile(List<string> dldFileList, string ftpLoginUser, string ftpLoginPwd, string dldFileSaveDir)
        {
            _dldFileList = dldFileList;
            _ftpLoginPwd = ftpLoginPwd;
            _ftpLoginUser = ftpLoginUser;
            _dldFileSaveDir = dldFileSaveDir;
        }

        /// <summary>
        /// 下载文件保存路径
        /// </summary>
        public string DldFileSaveDir
        {
            get
            {
                if (_dldFileSaveDir == string.Empty)
                    _dldFileSaveDir = AppDomain.CurrentDomain.BaseDirectory;

                //确保最后位为\
                if (_dldFileSaveDir.Substring(_dldFileSaveDir.Length - 1) != @"\")
                    _dldFileSaveDir  = "\\";

                return _dldFileSaveDir;
            }
            set
            {
                _dldFileSaveDir = value;
            }
        }

        /// <summary>
        /// FTP 登录用户
        /// </summary>
        public string FtpLoginUser
        {
            get
            {
                if (_ftpLoginUser == string.Empty)
                    throw new Exception("The Ftp LoginUser is null");

                return _ftpLoginUser;
            }
            set
            {
                _ftpLoginUser = value;
            }
        }

        /// <summary>
        /// FTP 登录密码
        /// </summary>
        public string FtpLoginPwd
        {
            get
            {
                if (_ftpLoginPwd == string.Empty)
                    throw new Exception("The Ftp LoginPwd is null");

                return _ftpLoginPwd;
            }
            set
            {
                _ftpLoginPwd = value;
            }
        }

        /// <summary>
        /// 下载FTP 文件每次读取大小
        /// </summary>
        public UInt32 FtpDldSize
        {
            get
            {
                return _ftpDldSize;
            }
            set
            {
                _ftpDldSize = value;
            }
        }

        #region IDldFile 成员

        public List<string> DldFilePathList
        {
            get
            {
                if (_dldFileList == null)
                    _dldFileList = new List<string>();

                return _dldFileList;
            }
            set
            {
                _dldFileList = value;
            }
        }

        /// <summary>
        /// 将指定的FTP 路径中目录路径和文件名称分离,获取文件名称
        /// </summary>
        /// <param name="ftpPath"></param>
        /// <returns></returns>
        public static string ConvertPathToFileName(string ftpPath)
        {
            string[] strArray = ftpPath.Split(new char[] { '/' });
            return strArray[strArray.Length - 1];
        }

        public void Download()
        {
            //_dldFileList = new List<string>();
            //_dldFileList.Add(@"ftp://192.168.0.21/incoming/1.txt");
            //_ftpLoginPwd = "IECAS12345678";
            //_ftpLoginUser = "xiangwei";

            string curFileName = string.Empty;
            try
            {
                FtpWebRequest request;
                FtpWebResponse response;

                foreach (string strFile in DldFilePathList)
                {
                    curFileName = ConvertPathToFileName(strFile);

                    //校验文件是否存在,如果存在给出提示用户是否覆盖
                    if (System.IO.File.Exists(DldFileSaveDir   curFileName))
                    {
                        //通过委托获取用户是否同意覆盖
                        if (!EIsReplace(strFile))
                        {
                            continue;
                        }
                    }
                    
                    request = (FtpWebRequest)WebRequest.Create(strFile);
                    request.KeepAlive = false;
                    request.Credentials = new NetworkCredential(_ftpLoginUser, _ftpLoginPwd);

                    using (response = (FtpWebResponse)request.GetResponse())
                    {
                        Stream data = response.GetResponseStream();

                        //文件大小
                        long fileLength = response.ContentLength;

                        //文件输出路径
                        string targetPath = DldFileSaveDir   curFileName;

                        //每次下载文件大小
                        byte[] byteBuffer = new byte[_ftpDldSize];

                        //当前已下载文件大小
                        long currentDldBytes = 0;
                        //当前已下载文件百分比
                        double currentPercentage = 0;

                        //开始下载文件时间
                        DateTime startTime = DateTime.Now;
                        TimeSpan timeSpan;
                        using (FileStream output = new FileStream(targetPath, FileMode.OpenOrCreate))
                        {
                            int bytesRead = 0;

                            //上报开始下载文件信息
                            EDldMessage("开始下载文件:"   curFileName   " 文件大小:"   fileLength );
                            do
                            {
                                //上报下载文件百分比
                                EDldPercentage(currentPercentage);

                                bytesRead = data.Read(byteBuffer, 0, byteBuffer.Length);
                                if (bytesRead > 0)
                                {
                                    output.Write(byteBuffer, 0, bytesRead);
                                }

                                currentDldBytes  = bytesRead;

                                currentPercentage = ((double)currentDldBytes / (double)fileLength) * 100;
                            }
                            while (bytesRead > 0);

                            timeSpan = DateTime.Now - startTime;
                            EDldMessage("下载"   curFileName   "完成,耗时:"   timeSpan.Minutes.ToString()   " 分 "   timeSpan.Seconds   " 秒 "   timeSpan.Milliseconds   "毫秒");
                        }
                    }
                }
            }
            catch (WebException e)
            {
                throw new WebException("Download "   curFileName   " error has occored!The exception message is "   e.Message);
            }
        }

        #endregion

        #region IDldFile 成员

        public event DelegateIsReplace EIsReplace;

        public event DelegateDldPercentage EDldPercentage;

        public event DelegateDldMessage EDldMessage;

        #endregion
    }
}