实现思路:通过访问FTP站点,将站点中的文件下载至软件指定位置。
第一步:FTP站点中导入需要下载更新的程序文件,并添加配置文件(配置下载后文件的下载路径),如下图所示:
第二步:Winfrom程序读取FTP站点服务下载配置文件,解析需要下载的文件列表
第三步:循环下载更新程序文件,下载至指定位置即可
IIS中创建FTP站点略(测试访问如下图)
具体实现可参考如下所示代码:
FTP下载操作方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | /// <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解析方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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 ; } } } |
测试调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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( "更新成功" ); } |
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本