(轉)FTP操作類,從FTP下載檔案

來源:互聯網
上載者:User

標籤:

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下載檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.