准备做个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;
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值