c#基于ftp自动升级 自动更新 自动下载
来源:互联网 发布:淘宝亲宝贝怎么入驻 编辑:程序博客网 时间:2024/05/17 08:45
这是一个c#通用的ftp下载程序,源代码请在http://download.csdn.net/detail/gdcyh/4075015下载
开发:陈炎和 2012.2.14
原理:
1.首先读取本地UpdateSetting.Client.xml配置文件(下称客户配置)
2.以客户配置节[Main][ftpServerIP]指定的地址连接ftp服务器(必须支持匿名)
3.取服务器端由客户配置节[Main][exefile]指定的执行程序名同名(或当前执行文件名同名)目录下的UpdateSetting.Svc.xml(下称服务配置)
4.比较两个配置文件的[Main][Version]节,若不相同则下载由服务配置节[Main][FileList]指定的文件及目录
5.下载时先保存到本地\temp_update目录,成功下载后再覆盖原有文件
使用主法
1.设置ftp服务器,建立与主程序的执行文件同名的目录,并将最新版本软件拷入,修改UpdateSetting.Svc.xml与UpdateSetting.Client.xm中的[Main][Version]不一致
2.执行下面步骤3或4
3.配置UpdateSetting.Client.xml中的[Main][exefile]节为主程序的执行文件名
4.将当前ftp下载的.exe文件重命名为主程序的执行文件名,删除UpdateSetting.Client.xml中的[Main][exefile]节
同时将ftp服务器端的主程序重命名,增加一个.exe后缀
部份代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace ftp下载
{
public class FtpUpdate : IDisposable
{
private XmlDocument _XmlClient;//客户端配置文件
private XmlDocument _XmlServer;//服务器端配置文件
private FtpWeb _ftpWeb;
private List<clsFileInfo> _list;
private string _currentDirectory;
private string _tmpPath;
frmUpdate _frmUpdate ;
private string _strClientFileName;
/// <summary>
/// 取配置文件中的主程序名
/// </summary>
public string exefile
{
get;
set;
}
public void DownLoad()
{
try
{
_frmUpdate.lFile.Text = "准备下载文件";
_frmUpdate.Show();
Application.DoEvents();
string ftpServerIP = GetXmlOfClient("ftpServerIP");
string fileName = System.IO.Path.GetFileNameWithoutExtension( exefile == string.Empty? Application.ExecutablePath:exefile) ;// 没有扩展名的文件名
_ftpWeb = new FtpWeb(ftpServerIP, fileName , "", "");//以文件名同名的目录
_XmlServer = new XmlDocument();
_XmlServer.Load(_ftpWeb.Download("UpdateSetting.Svc.xml"));
this.GetList();
if (_list.Count == 0) return ;
_frmUpdate.progressBar.Maximum = _list.Count;
Application.DoEvents();
if (Directory.Exists(_tmpPath)) Directory.Delete(_tmpPath, true);
Directory.CreateDirectory(_tmpPath);
this.DownLoading();
_frmUpdate.lFile.Text = "下载完毕,更新本地文件请稍候...";
Application.DoEvents();
this.UpDateFile();
_frmUpdate.lFile.Text = "下载完成";
Application.DoEvents();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
public FtpUpdate()
{
_list = new List<clsFileInfo>();
_currentDirectory = Directory.GetCurrentDirectory();
_tmpPath = _currentDirectory + @"\Temp_Update";
_frmUpdate = new frmUpdate()
{
//progressBar = { Maximum = _list.Count },
lFile = { Text = "准备下载文件..." }
};
_strClientFileName = Application.StartupPath + @"\UpdateSetting.Client.xml";
_XmlClient = new XmlDocument();
_XmlClient.Load(_strClientFileName);
exefile = this.GetXmlOfClient("exefile");
_frmUpdate.Title = this.GetXmlOfClient("title");
}
public void Dispose()
{
try
{
_frmUpdate.Hide();
_frmUpdate.Dispose();
_frmUpdate = null;
if (Directory.Exists(_tmpPath)) Directory.Delete(_tmpPath, true);
}
catch
{ }
}
/// <summary>
/// 取下载文件列表
/// </summary>
private void GetList()
{
_list.Clear();
if (this.GetVersionOfServer() == this.GetVersionOfClient()) return;
List<string> source = GetUpdateFileList();
if ((source == null) || (source.Count == 0)) return ;
int length = 0;
clsFileInfo item = null;
foreach (string str in source)
{
item = new clsFileInfo();
length = str.LastIndexOf(@"\");
if (length >= 0)
{
item.strSubDirectory = str.Substring(0, length);
item.strFileName = str.Substring(length + 1);
}
else
{
item.strSubDirectory = string.Empty;
item.strFileName = str;
}
_list.Add(item);
}
}
/// <summary>
/// 下载所有文件到临时目录
/// </summary>
private void DownLoading()
{
clsFileInfo item;
for (int i = 0; i < _list.Count; i++)
{
item = _list[i];
_frmUpdate.lFile.Text = "更新进度(" + Convert.ToString((int)(i + 1)) + "/" + _list.Count.ToString() + ") : " + item.strFileName;
Application.DoEvents();
if ((item.strSubDirectory != string.Empty) && !Directory.Exists(_tmpPath + @"\" + item.strSubDirectory))
{
Directory.CreateDirectory(_tmpPath + @"\" + item.strSubDirectory);
}
try
{
_ftpWeb.Download(_tmpPath,item.strSubDirectory + @"\" + item.strFileName);
item.strTempFilePath = _tmpPath + @"\" + item.strSubDirectory + @"\" + item.strFileName;
item.strWorkFilePath = _currentDirectory + @"\" + item.strSubDirectory + @"\" + item.strFileName;
}
catch
{
MessageBox.Show("下载文件: " + item.strFileName + " 失败。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
_frmUpdate.progressBar.Value = i + 1;
Application.DoEvents();
}
}
/// <summary>
/// 从临时文件更新本地文件
/// </summary>
private void UpDateFile()
{
foreach (clsFileInfo info in _list)
{
if ((info.strSubDirectory != string.Empty) && !Directory.Exists(_currentDirectory + @"\" + info.strSubDirectory))
{
Directory.CreateDirectory(_currentDirectory + @"\" + info.strSubDirectory);
}
if (File.Exists(info.strWorkFilePath))
{
File.SetAttributes(info.strWorkFilePath, FileAttributes.Normal);
File.Delete(info.strWorkFilePath);
}
File.Move(info.strTempFilePath, info.strWorkFilePath);
}
UpdateVersionOfClient(GetVersionOfServer());
}
/// <summary>
/// 取客户端版本号
/// </summary>
/// <param name="p_strKey"></param>
/// <returns></returns>
private string GetVersionOfClient()
{
return GetXmlOfClient("Version");
}
/// <summary>
/// 取客户端xml键值
/// </summary>
/// <param name="p_strKey">键</param>
/// <returns></returns>
private string GetXmlOfClient(string p_strKey)
{
string str = string.Empty;
XmlElement element = _XmlClient["Main"][p_strKey];
if (element != null)
{
str = element.Attributes["value"].Value.Trim();
}
element = null;
return str;
}
/// <summary>
/// 保存客户端版本号
/// </summary>
/// <param name="p_strVersion"></param>
private void UpdateVersionOfClient(string p_strVersion)
{
XmlElement element =_XmlClient["Main"]["Version"];
if (element != null)
{
element.Attributes["value"].Value = p_strVersion;
_XmlClient.Save("UpdateSetting.Client.xml");
}
element = null;
}
/// <summary>
/// 取待下载文件列表
/// </summary>
/// <returns></returns>
private List<string> GetUpdateFileList()
{
List<string> list = new List<string>();
XmlNodeList elementsByTagName = this._XmlServer["Main"]["FileList"].GetElementsByTagName("File");
if ((elementsByTagName != null) && (elementsByTagName.Count != 0))
{
XmlNode node = null;
for (int i = 0; i < elementsByTagName.Count; i++)
{
node = elementsByTagName.Item(i);
if (node.Attributes["status"].Value.Trim() == "1")
{
list.Add(node.Attributes["name"].Value.Trim());
}
}
node = null;
}
return list;
}
/// <summary>
/// 取服务器端版本号
/// </summary>
/// <returns></returns>
private string GetVersionOfServer()
{
string version = "";
XmlElement element = this._XmlServer["Main"]["Version"];
if (element != null)
{
version = element.Attributes["value"].Value.Trim();
}
element = null;
return version;
}
}
public class clsFileInfo
{
// Properties
public byte[] bytFileValue { get; set; }
public string strFileName { get; set; }
public string strSubDirectory { get; set; }
public string strTempFilePath { get; set; }
public string strWorkFilePath { get; set; }
}
}
- c#基于ftp自动升级 自动更新 自动下载
- C#基于ftp自动更新、自动下载
- 软件自动升级(旧版) 有新的用c#基于ftp自动更新
- C# 利用FTP自动下载xml文件后利用 FileSystemWatcher 监控目录下文件变化并自动更新数据库
- ftp 自动升级
- 自动更新+下载后自动安装
- C# 实现在线软件自动升级自动下载更新文件
- c#自动升级
- C# 自动升级
- c# winform 自动升级
- C# 自动升级程序代码
- expect实现ftp自动下载
- ftp自动下载shell脚本
- 基于TCP网络通信的自动升级程序源码分析-启动升级文件下载程序
- C#实现软件自动升级
- C#实现软件自动升级
- C#实现软件自动升级
- C#实现软件自动升级
- 串口一拖二——硬件实现、软件使用
- ORA-00600: internal error code, arguments: [19004]
- How to Uninstall Ad-Aware Free Internet Security
- 好扯淡的2416 处理器,手册写得好猥琐啊.
- 在iOS上实现Cover Flow (五)
- c#基于ftp自动升级 自动更新 自动下载
- 可扩展性设计模式(zz)
- 常用英文单词翻译
- CreateThread,_beginthread,_beginthreadx的区别
- 如何使用Java操作LDAP之LDAP连接(一)
- 几个 2416 wince问题 .
- Java或Web中解决所有路径问题
- C语言中的转义字符
- SQL基本操作