C# Winform 中使用FTP实现软件自动更新功能

实现思路:通过访问FTP站点,将站点中的文件下载至软件指定位置。

第一步:FTP站点中导入需要下载更新的程序文件,并添加配置文件(配置下载后文件的下载路径),如下图所示:

第二步:Winfrom程序读取FTP站点服务下载配置文件,解析需要下载的文件列表

第三步:循环下载更新程序文件,下载至指定位置即可

IIS中创建FTP站点略(测试访问如下图)

具体实现可参考如下所示代码:

FTP下载操作方法:

 /// <summary>
        /// 从ftp服务器上下载文件的功能  
        /// </summary>
        /// <param name="fileName">文件名称</param>
        public void Download(string fileName)
        {
            FtpWebRequest reqFTP;
            try
            {
                string filePath = Application.StartupPath;
                FileStream outputStream = new FileStream(filePath + "\" + fileName, FileMode.Create);
                string str=FTPFilePath + ":" + FtpServerPort + "/" + fileName;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(str));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(FtpServerUserName, FtpServerPassword);
                reqFTP.UsePassive = false;
                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)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 从ftp服务器上下载文件的功能  
        /// </summary>
        /// <param name="fileName">文件名称</param>
        /// <param name="targetPath">存放目标位置+文件名称</param>
        public void Download(string fileName,string targetPath)
        {
            FtpWebRequest reqFTP;
            try
            {
                string filePath = Application.StartupPath;
                //FileStream outputStream = new FileStream(filePath + "\" + fileName, FileMode.Create);
                FileStream outputStream = new FileStream(targetPath, FileMode.Create);
                string str = FTPFilePath + ":" + FtpServerPort + "/" + fileName;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(str));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(FtpServerUserName, FtpServerPassword);
                reqFTP.UsePassive = false;
                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)
            {
                throw ex;
            }
        }

  

XML解析方法:

using System.Collections.Generic;
using System.Xml;

namespace AutoUpdate
{
    /// <summary>
    /// 用于XML操作
    /// </summary>
    public class XmlHelper
    {
        private static XmlHelper instance;
        public static XmlHelper Instance
        {
            get
            {
                if (instance == null) instance = new XmlHelper();
                return XmlHelper.instance;
            }
        }

        /// <summary>
        /// 获取指定节点所有子节点相关属性
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="nodePath">节点路径</param>
        /// <returns>List<UpdateList></returns>
        public List<UpdateList> GetUpdateList(string path, string selectNode)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlNode node = doc.SelectSingleNode(selectNode);
            List<UpdateList> list = new List<UpdateList>();
            foreach (XmlNode item in node)
            {
                UpdateList config = new UpdateList();
                config.NAME = item.Attributes["NAME"].Value;
                config.PATH = item.Attributes["PATH"].Value;

                list.Add(config);
            }
            return list;
        }

    }

    /// <summary>
    /// config.xml item节点属性描述
    /// </summary>
    public class UpdateList
    {
        public string PATH { get; set; }
        public string NAME { get; set; }
    }
} 

测试调用:

private void btnUpdate_Click(object sender, EventArgs e)
        {
            //第一步下载需要更新的配置文件
            string filepath = "UpdateList.xml";
            UpdateHelper.Instance.Download(filepath);
            //第二步 提供并解析配置文件(获取需要更新的文件名称、文件更新后的路径)
            string path = Application.StartupPath + "\UpdateList.xml";
            List<UpdateList> list = XmlHelper.Instance.GetUpdateList(path, "Root");
            //第三步 循环下载文件到指定路径
            foreach (UpdateList item in list)
            {
                if (string.IsNullOrEmpty(item.PATH)) UpdateHelper.Instance.Download(item.NAME);//下载至根目录
                else //下载至指定目录
                {
                    string targetPath = Application.StartupPath + "\" + item.PATH + "\" + item.NAME;
                    UpdateHelper.Instance.Download(item.NAME, targetPath);
                }
            }
            MessageBox.Show("更新成功");
        }

  

原文地址:https://www.cnblogs.com/YYkun/p/10471490.html