载入中,请稍候……

FTP操作范例

Admin 于 2008-09-29 03:42:37 发表.NET

订阅: http://www.miniboke.com/Feed/Article_59.aspx
引用: http://www.miniboke.com/Trackback/ujkULrukFFLTfvonrHPK.aspx (UTF-8)
CFileFind类的使用总结 < FTP操作范例 > 通过反射调用private方法

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Net; 
  4. using System.IO; 
  5. using System.Windows.Forms; 
  6. using System.Text; 
  7.  
  8. namespace FTPDownLoad 
  9.     class FtpFileInfo 
  10.     { 
  11.         public string FileName; 
  12.         public DateTime ModifyDateTime; 
  13.         public bool IsDirectory; 
  14.     } 
  15.     class FtpHelper 
  16.     { 
  17.         private string ftpServer; 
  18.         private string userName; 
  19.         private string password; 
  20.         FtpWebRequest ftpRequest = null
  21.         private string errMsg; 
  22.         public string ErrMsg 
  23.         { 
  24.             get { return errMsg; } 
  25.             set { errMsg = value; } 
  26.         } 
  27.         public bool IsAnonymous 
  28.         { 
  29.             get 
  30.             { 
  31.                 return !(userName != null && userName.Trim() != String.Empty 
  32.                         && password != null && password.Trim() != string.Empty); 
  33.             } 
  34.         } 
  35.  
  36.         public FtpHelper(string ftpServer, string userName, string password) 
  37.         { 
  38.             this.ftpServer = ftpServer; 
  39.             this.userName = userName; 
  40.             this.password = password; 
  41.         } 
  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.         /**/ 
  104.         /// <summary> 
  105.         /// Download 
  106.         /// </summary> 
  107.         /// <param name="serverPath"></param> 
  108.         /// <param name="serverFileName"></param> 
  109.         /// <param name="localPath"></param> 
  110.         /// <param name="localFileName"></param> 
  111.         /// <returns></returns> 
  112.         public bool Download(string serverPath, string serverFileName, string localPath, string localFileName) 
  113.         { 
  114.             if (!Directory.Exists(localPath)) 
  115.             { 
  116.                 //TODO: 创建文件夹 
  117.                 errMsg = "本地路径不存在: " + localPath; 
  118.                 return false
  119.             } 
  120.             FileStream outputStream = null
  121.             Stream ftpStream = null
  122.             try 
  123.             { 
  124.                 outputStream = new FileStream(localPath + "\\" + localFileName, FileMode.Create); 
  125.                 if (outputStream == null
  126.                 { 
  127.                     errMsg = "无法创建本地文件"
  128.                     return false
  129.                 } 
  130.                 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServer + serverPath + "/" + serverFileName)); 
  131.                 if (ftpRequest == null
  132.                 { 
  133.                     errMsg = "无法连接服务器"
  134.                     return false
  135.                 } 
  136.                 ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; 
  137.                 ftpRequest.UseBinary = true
  138.                 //用户验证 
  139.                 if (!IsAnonymous) 
  140.                 { 
  141.                     ftpRequest.Credentials = new NetworkCredential(userName, password); 
  142.                 } 
  143.                 ftpStream = ftpRequest.GetResponse().GetResponseStream(); 
  144.                 int bufferSize = 2048; 
  145.                 int readCount; 
  146.                 byte[] buffer = new byte[bufferSize]; 
  147.  
  148.                 while ((readCount = ftpStream.Read(buffer, 0, bufferSize)) > 0) 
  149.                 { 
  150.                     outputStream.Write(buffer, 0, readCount); 
  151.                 } 
  152.             } 
  153.             catch (Exception ex) 
  154.             { 
  155.                 errMsg = ex.ToString(); 
  156.                 return false
  157.             } 
  158.             finally 
  159.             { 
  160.                 if (ftpStream != null) ftpStream.Close(); 
  161.                 if (outputStream != null) outputStream.Close(); 
  162.             } 
  163.             FileInfo fi = new FileInfo(localPath + "\\" + localFileName); 
  164.             fi.LastWriteTime = GetFileModifyTime(serverPath, serverFileName); 
  165.             return true
  166.         } 
  167.  
  168.         public void DownLoadDirectory(String ServerPath, String LocalPath) 
  169.         { 
  170.             //取服务器端要下载的目录里的文件列表 
  171.             List<FtpFileInfo> serFileList = GetFilesList(ServerPath); 
  172.             foreach (FtpFileInfo ftpFile in serFileList) 
  173.             { 
  174.                 if (ftpFile.IsDirectory) continue
  175.                 DateTime modifyTime = ftpFile.ModifyDateTime; 
  176.                 string localFileName = modifyTime.ToString("yyyyMMddHHmmss") + "_" + ftpFile.FileName; 
  177.  
  178.                 // 如果本地不存在该文件,则说明该文件是比较新的文件,需要下载. 
  179.                 if (!File.Exists(LocalPath + "\\" + localFileName)) 
  180.                 { 
  181.                     if (!Download(ServerPath, ftpFile.FileName, LocalPath, localFileName)) 
  182.                     { 
  183.                         MessageBox.Show("下载文件出错!\r\n错误原因: " + ErrMsg); 
  184.                     } 
  185.                 } 
  186.             } 
  187.         } 
  188.  
  189.         public DateTime GetFileModifyTime(string serverPath, string fileName) 
  190.         { 
  191.             Uri uri = new Uri("ftp://" + ftpServer + serverPath + "/" + fileName); 
  192.             DateTime dt = DateTime.Now; 
  193.             try 
  194.             { 
  195.                 ftpRequest = (FtpWebRequest)WebRequest.Create(uri); 
  196.                 if (!IsAnonymous) 
  197.                 { 
  198.                     ftpRequest.Credentials = new NetworkCredential(userName, password); 
  199.                 } 
  200.                 ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp; 
  201.                 ftpRequest.UseBinary = true
  202.                 ftpRequest.UsePassive = false
  203.                 dt = ((FtpWebResponse)ftpRequest.GetResponse()).LastModified; 
  204.             } 
  205.             catch (Exception ex) 
  206.             { 
  207.                 //TODO: 错误处理 
  208.                 throw ex; 
  209.             } 
  210.             return dt; 
  211.         } 
  212.     } 

 

被阅407次, 0投一票FTP
  • 看完了要说点啥么?
  • 昵称 (不填说不了话)
  • 信箱地址 (不会被公开,但是不填也说不了话)
  • 网址 (这个不填也成)

Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 迷你博客. All rights reserved.

粤ICP备07500939号