【FTP】C# System.Net.FtpClient庫連接ftp服務器(下載文件)


如果自己單槍匹馬寫一個連接ftp服務器代碼那是相當恐怖的(socket通信),有一個評價較高的dll庫可以供我們使用。

那就是System.Net.FtpClient,鏈接地址:https://netftp.codeplex.com

然后下載該資源,我們就可以使用它的函數了。這里介紹一下如何使用System.Net.FtpClient鏈接ftp服務器並下載服務器中的文件。

千萬別忘了添加引用——導入System.Net.FtpClient.dll.

還有就是 using System.Net.FtpClient;

            using System.Net;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.FtpClient;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace FTP_Client
{
    public class FTPConnection
    {
        
        public FTPConnection() { }

        /// <summary>
        /// 連接FTP服務器函數
        /// </summary>
        /// <param name="strServer">服務器IP</param>
        /// <param name="strUser">用戶名</param>
        /// <param name="strPassword">密碼</param>
        public bool FTPIsConnected(string strServer, string strUser, string strPassword)
        {
            using (FtpClient ftp = new FtpClient())
            {
                ftp.Host = strServer;
                ftp.Credentials = new NetworkCredential(strUser, strPassword);
                ftp.Connect();
                return ftp.IsConnected;
            }
        }

        
        /// <summary>
        /// FTP下載文件
        /// </summary>
        /// <param name="strServer">服務器IP</param>
        /// <param name="strUser">用戶名</param>
        /// <param name="strPassword">密碼</param>
        /// <param name="Serverpath">服務器路徑,例子:"/Serverpath/"</param>
        /// <param name="localpath">本地保存路徑</param>
        /// <param name="filetype">所下載的文件類型,例子:".rte"</param>
        public bool FTPIsdownload(string strServer, string strUser, string strPassword,string Serverpath, string localpath, string filetype)
        {
            
            FtpClient ftp = new FtpClient();
            ftp.Host = strServer;
            ftp.Credentials = new NetworkCredential(strUser, strPassword);
            ftp.Connect();

            string path = Serverpath;
            string destinationDirectory = localpath;
            List<string> documentname = new List<string>();
            bool DownloadStatus = false;

            if (Directory.Exists(destinationDirectory))
            {
                #region  從FTP服務器下載文件
                foreach (var ftpListItem in ftp.GetListing(path, FtpListOption.Modify | FtpListOption.Size)
                  .Where(ftpListItem => string.Equals(Path.GetExtension(ftpListItem.Name), filetype)))
                {
                    string destinationPath = string.Format(@"{0}\{1}", destinationDirectory, ftpListItem.Name);
                    using (Stream ftpStream = ftp.OpenRead(ftpListItem.FullName))
                    using (FileStream fileStream = File.Create(destinationPath, (int)ftpStream.Length))
                    {
                        var buffer = new byte[200 * 1024];
                        int count;
                        while ((count = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            fileStream.Write(buffer, 0, count);
                        }
                    }
                    documentname.Add(ftpListItem.Name);
                }
                #endregion

                #region  驗證本地是否有該文件
                string[] files = Directory.GetFiles(localpath, "*"+filetype);
                int filenumber = 0;
                foreach(string strfilename in files)
                {                    
                    foreach(string strrecievefile in documentname)
                    {
                        if (strrecievefile == Path.GetFileName(strfilename))
                        {
                            filenumber++;
                            break;
                        }
                    }
                }
                if(filenumber==documentname.Count)
                {
                    DownloadStatus = true;
                }              
                #endregion
            }         
            return DownloadStatus;
        }

    }
}

 注意:如果存放在服務器的文件是放在根目錄下,那么服務器路徑只需填寫“/”,即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM