• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 124,  comments - 29,  trackbacks - 0
              1using System;
              2using System.Collections.Generic;
              3using System.Net;
              4using System.IO;
              5using System.Windows.Forms;
              6using System.Text;
              7
              8namespace FTPDownLoad
              9{
             10    class FtpFileInfo
             11    {
             12        public string FileName;
             13        public DateTime ModifyDateTime;
             14        public bool IsDirectory;
             15    }

             16    class FtpHelper
             17    {
             18        private string ftpServer;
             19        private string userName;
             20        private string password;
             21        FtpWebRequest ftpRequest = null;
             22        private string errMsg;
             23        public string ErrMsg
             24        {
             25            get return errMsg; }
             26            set { errMsg = value; }
             27        }

             28        public bool IsAnonymous
             29        {
             30            get
             31            {
             32                return !(userName != null && userName.Trim() != String.Empty
             33                        && password != null && password.Trim() != string.Empty);
             34            }

             35        }

             36
             37        public FtpHelper(string ftpServer, string userName, string password)
             38        {
             39            this.ftpServer = ftpServer;
             40            this.userName = userName;
             41            this.password = password;
             42        }

             43
             44        /// <summary>
             45        /// 取得服務器端的文件鏈表
             46        /// </summary>
             47        /// <param name="serverPath"></param>
             48        /// <returns></returns>

             49        public List<FtpFileInfo> GetFilesList(string serverPath)
             50        {
             51            List<FtpFileInfo> fileInfoList = new List<FtpFileInfo>();
             52            Uri uri = new Uri("ftp://" + ftpServer + serverPath);
             53            StreamReader sr = null;
             54            try
             55            {
             56                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(uri);
             57                if (ftpRequest == nullthrow new Exception("無法打開ftp服務器連接");
             58                ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;   //列表   
             59                if (!IsAnonymous)
             60                {
             61                    ftpRequest.Credentials = new NetworkCredential(userName, password);
             62                }

             63
             64                sr = new StreamReader(ftpRequest.GetResponse().GetResponseStream());
             65                while (!sr.EndOfStream)//讀取列表   
             66                {
             67                    //System.Diagnostics.Debug.WriteLine(sr.ReadLine());
             68                    char[] splitChar = ' ' };
             69                    string[] tmpArray = sr.ReadLine().Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
             70                    if (tmpArray.Length != 9)
             71                    {
             72                        continue;
             73                    }

             74                    FtpFileInfo ffi = new FtpFileInfo();
             75                    ffi.IsDirectory = tmpArray[0].StartsWith("d");
             76                    if (!ffi.IsDirectory)
             77                    {
             78                        ffi.FileName = tmpArray[8];
             79                        fileInfoList.Add(ffi);
             80                    }

             81                    else
             82                    {
             83                        continue;
             84                    }

             85                }

             86            }

             87            catch (Exception ex)
             88            {
             89                //TODO: 異常處理.
             90                throw ex;
             91            }

             92            finally
             93            {
             94                if (sr != null) sr.Close();
             95            }

             96
             97            foreach (FtpFileInfo ffi in fileInfoList)
             98            {
             99                ffi.ModifyDateTime = this.GetFileModifyTime(serverPath, ffi.FileName);
            100            }

            101            return fileInfoList;
            102        }

            103        /// <summary>
            104        /// Download
            105        /// </summary>
            106        /// <param name="serverPath"></param>
            107        /// <param name="serverFileName"></param>
            108        /// <param name="localPath"></param>
            109        /// <param name="localFileName"></param>
            110        /// <returns></returns>

            111        public bool Download(string serverPath, string serverFileName, string localPath, string localFileName)
            112        {
            113            if (!Directory.Exists(localPath))
            114            {
            115                //TODO: 創建文件夾
            116                errMsg = "本地路徑不存在: " + localPath;
            117                return false;
            118            }

            119            FileStream outputStream = null;
            120            Stream ftpStream = null;
            121            try
            122            {
            123                outputStream = new FileStream(localPath + "\\" + localFileName, FileMode.Create);
            124                if (outputStream == null)
            125                {
            126                    errMsg = "無法創建本地文件";
            127                    return false;
            128                }

            129                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServer + serverPath + "/" + serverFileName));
            130                if (ftpRequest == null)
            131                {
            132                    errMsg = "無法連接服務器";
            133                    return false;
            134                }

            135                ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            136                ftpRequest.UseBinary = true;
            137                //用戶驗證
            138                if (!IsAnonymous)
            139                {
            140                    ftpRequest.Credentials = new NetworkCredential(userName, password);
            141                }

            142                ftpStream = ftpRequest.GetResponse().GetResponseStream();
            143                int bufferSize = 2048;
            144                int readCount;
            145                byte[] buffer = new byte[bufferSize];
            146
            147                while ((readCount = ftpStream.Read(buffer, 0, bufferSize)) > 0)
            148                {
            149                    outputStream.Write(buffer, 0, readCount);
            150                }

            151            }

            152            catch (Exception ex)
            153            {
            154                errMsg = ex.ToString();
            155                return false;
            156            }

            157            finally
            158            {
            159                if (ftpStream != null) ftpStream.Close();
            160                if (outputStream != null) outputStream.Close();
            161            }

            162            FileInfo fi = new FileInfo(localPath + "\\" + localFileName);
            163            fi.LastWriteTime = GetFileModifyTime(serverPath, serverFileName);
            164            return true;
            165        }

            166
            167        public  void DownLoadDirectory(String ServerPath, String LocalPath)
            168        {
            169            //取服務器端要下載的目錄里的文件列表
            170            List<FtpFileInfo> serFileList = GetFilesList(ServerPath);
            171            foreach (FtpFileInfo ftpFile in serFileList)
            172            {
            173                if (ftpFile.IsDirectory) continue;
            174                DateTime modifyTime = ftpFile.ModifyDateTime;
            175                string localFileName = modifyTime.ToString("yyyyMMddHHmmss"+ "_" + ftpFile.FileName;
            176
            177                // 如果本地不存在該文件,則說明該文件是比較新的文件,需要下載.
            178                if (!File.Exists(LocalPath + "\\" + localFileName))
            179                {
            180                    if (!Download(ServerPath, ftpFile.FileName, LocalPath, localFileName))
            181                    {
            182                        MessageBox.Show("下載文件出錯!\r\n錯誤原因: " + ErrMsg);
            183                    }

            184                }

            185            }

            186        }

            187           
            188        public DateTime GetFileModifyTime(string serverPath, string fileName)
            189        {
            190            Uri uri = new Uri("ftp://" + ftpServer + serverPath + "/" + fileName);
            191            DateTime dt = DateTime.Now;
            192            try
            193            {
            194                ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
            195                if (!IsAnonymous)
            196                {
            197                    ftpRequest.Credentials = new NetworkCredential(userName, password);
            198                }

            199                ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
            200                ftpRequest.UseBinary = true;
            201                ftpRequest.UsePassive = false;
            202                dt = ((FtpWebResponse)ftpRequest.GetResponse()).LastModified;
            203            }

            204            catch (Exception ex)
            205            {
            206                //TODO: 錯誤處理
            207                throw ex;
            208            }

            209            return dt;
            210        }

            211    }

            212}
            posted on 2008-09-26 17:36 天書 閱讀(1060) 評論(0)  編輯 收藏 引用

            <2008年10月>
            2829301234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            常用鏈接

            留言簿(5)

            隨筆檔案

            文章分類

            文章檔案

            好友的Bolg

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久av高潮av无码av喷吹| 久久精品国产99久久无毒不卡| 欧美精品久久久久久久自慰| 少妇人妻88久久中文字幕| 久久99热只有频精品8| 77777亚洲午夜久久多喷| 久久久国产精品网站| 亚洲欧美精品一区久久中文字幕| 精品国产日韩久久亚洲| …久久精品99久久香蕉国产| 精品国产一区二区三区久久蜜臀| 久久综合亚洲鲁鲁五月天| 久久精品国产一区二区三区日韩| 亚洲欧洲久久久精品| AV色综合久久天堂AV色综合在| 7国产欧美日韩综合天堂中文久久久久| 日韩美女18网站久久精品| 精品人妻久久久久久888| 精品久久国产一区二区三区香蕉| 99久久99久久精品国产片果冻| 丁香五月综合久久激情| 日韩精品久久久久久久电影蜜臀| 久久高清一级毛片| 久久国产色AV免费观看| 久久这里的只有是精品23| 99久久精品免费观看国产| 老色鬼久久亚洲AV综合| 精品久久久久久无码不卡| 国产AV影片久久久久久| 国内精品伊人久久久久AV影院| 伊人久久大香线蕉成人| 久久久噜噜噜久久中文字幕色伊伊 | 久久精品www人人爽人人| 人人狠狠综合88综合久久| 久久91亚洲人成电影网站| 久久人人爽人人爽人人AV东京热 | 久久99精品免费一区二区| 99久久精品国产高清一区二区| 少妇人妻综合久久中文字幕| 中文精品99久久国产 | 精品久久久久久国产三级|