標籤:
using System;using System.Collections.Generic;using System.Text;using System.IO;using System.Net;using System.Windows.Forms;namespace PCMDataImporter{ /// <summary> /// 封裝了FTP的兩個操作:下載檔案Download()和擷取FTP伺服器上檔案清單資訊GetFileList() /// </summary> public class ftpOperater { private string ftpServerIP; private string ftpUser; private string ftpPwd; private SystemParaReader spReader; public ftpOperater() { spReader = new SystemParaReader(); this.ftpServerIP = spReader.GetSysParaValue("ftpServer"); this.ftpUser = spReader.GetSysParaValue("ftpUser"); this.ftpPwd = spReader.GetSysParaValue("ftpPwd"); } /// <summary> /// 擷取ftp伺服器上的檔案資訊 /// </summary> /// <returns>儲存了所有檔案資訊的字串數組</returns> public string[] GetFileList() { string[] downloadFiles; StringBuilder result = new StringBuilder(); FtpWebRequest reqFTP; try { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/")); reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd); reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; WebResponse response = reqFTP.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string line = reader.ReadLine(); while (line != null) { result.Append(line); result.Append("\n"); line = reader.ReadLine(); } result.Remove(result.ToString().LastIndexOf(‘\n‘), 1); reader.Close(); response.Close(); return result.ToString().Split(‘\n‘); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show("擷取檔案資訊失敗:"+ex.Message,"操作失敗",MessageBoxButtons.OK,MessageBoxIcon.Error); downloadFiles = null; return downloadFiles; } } /// <summary> /// 擷取FTP上指定檔案的大小 /// </summary> /// <param name="filename">檔案名稱</param> /// <returns>檔案大小</returns> public long GetFileSize(string filename) { FtpWebRequest reqFTP; long fileSize = 0; try { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + filename)); reqFTP.Method = WebRequestMethods.Ftp.GetFileSize; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); fileSize = response.ContentLength; ftpStream.Close(); response.Close(); } catch (Exception ex) { MessageBox.Show("擷取檔案大小時,出現異常:\n" + ex.Message, "擷取檔案大小失敗!", MessageBoxButtons.OK, MessageBoxIcon.Error); } return fileSize; } /// <summary> /// 實現ftp下載操作 /// </summary> /// <param name="filePath">儲存到本地的檔案名稱</param> /// <param name="fileName">遠程檔案名稱</param> public void Download(string filePath, string fileName) { FtpWebRequest reqFTP; try { //filePath = <<The full path where the file is to be created.>>, //fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>> FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName)); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }}
(轉)FTP操作類,從FTP下載檔案