实现思路:通过访问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("更新成功");
}

  

最新文章

  1. js 页面刷新location.reload和location.replace的区别小结
  2. Mark Down绘图语法
  3. C#中语音合成简单使用
  4. linux时间不同步问题
  5. HFS 2.3x 远程命令执行(抓鸡黑客末日)
  6. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Capture the Flag
  7. python常用内置函数
  8. Java中hashcode,equals和==
  9. phpcms v9 读取地区联动菜单缓存文件
  10. NYOJ128 前缀式计算(栈的运用)
  11. WordPress中函数钩子hook的作用及基本用法
  12. 注册Azure AD 应用程序
  13. 关于win8的各种版本的区别
  14. [Swift]LeetCode913.猫与老鼠 | Cat and Mouse
  15. 前端 CSS 目录
  16. axios封装get方法和post方法
  17. delimiter 与 存储过程
  18. Java元注解—— @Retention @Target @Document @Inherited
  19. jmeter的使用
  20. TZOJ 5291 游戏之合成(快速幂快速乘)

热门文章

  1. 根据图片URL获取图片的尺寸【Swift语言实现】
  2. myeclipse 修改用户名密码
  3. python网络编程(UDP+广播)
  4. linux date使用
  5. 模拟ios应用加载页面
  6. VMware三种网络介绍
  7. 设置Ubuntu右侧显示扩展屏幕。。。
  8. 百度翻译新API C#版在 winform,Asp.Net的小程序
  9. MDK5报错missing closing quote
  10. C语言链表:逆序建立单链表