[原创]C/S模式开发中利用WebClient自动升级

来源:互联网 发布:陕西信合网络 编辑:程序博客网 时间:2024/05/17 02:49

第一次在cnblogs发文章,没的什么经验,说的不好请大家见谅.

之前开发一套应用系统,采用了C/S模式,对客户端的应用程序自动升级提出了一些要求。
我便利用WebClient做了一下这个自动升级系统,在应用中还算不错。今日特地贡献出来,希望给大家一些借鉴。
系统分为3个部分:
1.Update.dll:供主应用程序调用的检测升级模块。
2.Update.exe:升级的主程序.
3.升级文件的服务器端配置文件和更新文件.需要WEB服务器支持。


第一部分:服务器存放的配置文件。
假设配置文件放在一个web服务器的某个目录下面。如:http://192.168.0.1/UpdateFiles
目录结构如下:
update.exe
update.xml
update/目录存放需要更新的文件列表

update.xml文件为升级调用的配置文件,如下:
<?xml version="1.0" encoding="utf-8" ?>
<Product>
  <Version>1.6.2.1</Version>
  <Description>2007.4.28升级版本</Description>
  <FileList>
    <Item Name="mis.exe" FileURL="http://192.168.0.1/UpdateFiles/Updatetmis.exe" Folder="" />
    <Item Name="config.ini" FileURL="http://192.168.0.1/UpdateFiles/Update/config.ini" Folder="" />   
    <Item Name="update.dll" FileURL="http://192.168.0.1/UpdateFiles/Update/update.dll" Folder="" />        
  </FileList>
</Product>
配置文件主要提供了当前服务器存放的系统版本号,已经相关的文件列表。相信大家都明白什么意思,不多说了。

第二部分:检测升级Update.dll
升级模块为了方便程序调用,单独封装在一个dll中,主程序只需要直接引用调用即可。代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Net;
using System.Xml;
/*
 系统升级类
 */
namespace Update
{
    public class AutoUpdate
    {
        private WebClient client;
        private XmlDocument doc;
        public string Version = "";

        public void LoadUpdateXML()
        {
            //获取服务器信息
            client = new WebClient();           
            doc = new XmlDocument();
            try
            {
                doc.Load(client.OpenRead(@"http://192.168.0.1/UpdateFiles/Update.xml"));
                client = null;               
                //分析文件
                XmlNode node;
                node = doc.SelectSingleNode("Product/Version");
                if (node != null)
                {
                    Version = node.InnerText.Trim();
                }
            }
            catch
            {
                MessageBox.Show("无法取得更新文件!");
               
            }
        }

        /// <summary>
        /// 判断是否需要升级
        /// </summary>
        /// <returns></returns>
        public bool NeedUpdate()
        {
            LoadUpdateXML();
            //MessageBox.Show(Version + "||" + Application.ProductVersion);
            if (Version.Trim().CompareTo(Application.ProductVersion.Trim()) > 0)
                return true;
            return false;
        }
        /// <summary>
        /// 开始升级,
        /// </summary>
        public void Update()
        {
            if (NeedUpdate())
            {
                MessageBox.Show("已经有更新的程序,点击确定马上启动升级程序。");
                //下载服务器最新的升级程序,并且运行,退出本程序
                client = new WebClient();
                client.DownloadFile(@"http://192.168.0.1/UpdateFiles/update.exe", Application.StartupPath + "//update.exe");
                System.Diagnostics.Process.Start(Application.StartupPath + "//update.exe");               
                Application.ExitThread();
                Application.Exit();
            }
        }
    }
}

第三部分:更新程序update.exe
程序加载的时候从服务器读取文件列表,并且下载到本地执行目录,完成升级后自动启动主程序。
下载文件使用DownloadFile类,如下:
DownloadFile df = new DownloadFile();
// 文件的服务器地址
df.DownloadUrl = serverFile;
//下载本地的路径
df.DownloadFileName = localFile;
df.Download();

具体如何设计界面,因人而异,只是给出一个提示,请读者自己去完成。

主应用程序的调用Update.dll后如何检测呢?
Update.AutoUpdate up=new  Update.AutoUpdate();
if(up.NeedUpdate())
{
up.Update();

}

暂时就写到这里,有需要帮助的朋友请加我qq:38041762 或者email:gofixiao#126.com(请将#换成@)
补上demo:
/Files/gofixiao/Update_demo.rar

 
原创粉丝点击