打包好的应用程序进行实时更新

在应用程序的下载更新下载问题上,往往很难让版本得到统一,下面我就用些简单的方法实现版本对比,并确定是否更新的功能!

首先在解决方案下加个类库(update)用以添加并引用到winform应用程序登录页面中进行比较版本。

SoftUpdate.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Reflection;

namespace Update
{
/// <summary>
/// 更新完成触发的事件
/// </summary>
public delegate void UpdateState();


/// <summary>
/// 程序更新
/// </summary>
public class SoftUpdate
{
private string download;
private const string updateUrl = "http://192.168.100.45/update.xml"; //升级配置的XML文件地址

#region 构造函数
public SoftUpdate() { }
public SoftUpdate(string file, string softName)
{
this.LoadFile = file;
this.SoftName = softName;
}
#endregion

#region 属性
private string loadFile;
private string newVerson;
private string softName;
private bool isUpdate;
/// <summary>
/// 或取是否需要更新
/// </summary>
public bool IsUpdate
{
get
{
checkUpdate();
return isUpdate;
}
}

/// <summary>
/// 要检查更新的文件
/// </summary>
public string LoadFile
{
get { return loadFile; }
set { loadFile = value; }
}

/// <summary>
/// 程序集新版本
/// </summary>
public string NewVerson
{
get { return newVerson; }
}

/// <summary>
/// 升级的名称
/// </summary>
public string SoftName
{
get { return softName; }
set { softName = value; }
}


#endregion

/// <summary>
/// 更新完成时触发的事件
/// </summary>
public event UpdateState UpdateFinish;
private void isFinish()
{
if (UpdateFinish != null)
UpdateFinish();
}

/// <summary>
/// 下载更新
/// </summary>
public void Update()
{
try
{
if (!isUpdate)
return;
WebClient wc = new WebClient();
string filename = "WMSSetup";
string exten = download.Substring(download.LastIndexOf("."));
if (loadFile.IndexOf(@"\") == -1)
filename = "Update_" + Path.GetFileNameWithoutExtension(loadFile) + exten;
else
filename = Path.GetDirectoryName(loadFile) + "\\Update_" + Path.GetFileNameWithoutExtension(loadFile) + exten;
wc.DownloadFile(download, filename);
wc.Dispose();
isFinish();
}
catch
{
throw new Exception("更新出现错误,网络连接失败!");
}
}

/// <summary>
/// 检查是否需要更新
/// </summary>
public void checkUpdate()
{
try
{
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(updateUrl);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(stream);
XmlNode list = xmlDoc.SelectSingleNode("Update");
foreach (XmlNode node in list)
{
if (node.Name == "Soft" && node.Attributes["Name"].Value.ToLower() == SoftName.ToLower())
{
foreach (XmlNode xml in node)
{
if (xml.Name == "Verson")
newVerson = xml.InnerText;
else
download = xml.InnerText;
}
}
}

Version ver = new Version(newVerson);
Version verson = Assembly.LoadFrom(loadFile).GetName().Version;
int tm = verson.CompareTo(ver);

if (tm >= 0)
isUpdate = false;
else
isUpdate = true;
}
catch (Exception ex)
{
throw new Exception("更新出现错误,请确认网络连接无误后重试!");
}
}

/// <summary>
/// 获取要更新的文件
/// </summary>
/// <returns></returns>
public override string ToString()
{
return this.loadFile;
}



}
}

然后在服务器端有个xml文件进行版本的实时记录

 

<?xml version="1.0" encoding="utf-8" ?>
<Update>
<Soft Name="WMSSetup">
<Verson>1.0.1.2</Verson>
<DownLoad>http://192.168.100.45/WMSSetup.rar</DownLoad>
</Soft>
</Update>

然后在已有的登录界面加上

Login.CS

 

public login()
{
InitializeComponent();
checkUpdate();
}

public void checkUpdate()
{
SoftUpdate app = new SoftUpdate(Application.ExecutablePath, "WMSSetup");
app.UpdateFinish += new UpdateState(app_UpdateFinish);
try
{
if (app.IsUpdate && MessageBox.Show("检查到新版本,是否更新?", "Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{

Thread update = new Thread(new ThreadStart(app.Update));
update.Start();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

void app_UpdateFinish()
{
MessageBox.Show("更新完成,请重新启动程序!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

 

posted @ 2012-03-15 09:17  Mecry  阅读(480)  评论(0编辑  收藏  举报