C# 实现版本自动更新

 
查看文章
  
c#实现版本自动更新升级
2012-05-12 0:35

先新建一个类库

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

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

        private string download;
        private const string updateUrl = "升级配置的XML文件地址

        #region 构造函数
        public SoftUpdate() { }

        /// <summary>
        /// 程序更新
        /// </summary>
        /// <param name="file">要更新的文件</param>
        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 = "";
                string exten = download.Substring(download.LastIndexOf("."));
                if (loadFile.IndexOf(@"\") == -1)

                    filename = "Update_" + Path.GetFileNameWithoutExtension(loadFile) + exten;
                else
                    filename = Path.GetDirectoryName(loadFile) + "_" +

      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;
        }
    }
}

 

把代码编译为一个类库文件,通过程序引用就OK啦。
传入的参数已经有注释了。
下面是更新的XML文件类容,传到空间上面就可以了,得到XML文件的地址。
<?xml version="1.0" encoding="utf-8" ?>   

<Update> 

<Soft Name="BlogWriter"> 

 <Verson>1.0.1.2</Verson>   

<DownLoad>http://192.168.1.25/BlogWrite.rar</DownLoad>   

</Soft> 

</Update> 
程序更新调用方法:
 1、先引用上面的DLL。
 2、调用方法代码 如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Threading;

using System.Net;

using System.Xml;

using Update;

 

namespace up
{

    public partial class Form1 : Form
    {

        public Form1()
        {

            InitializeComponent();

            checkUpdate();

        }

 

        public void checkUpdate()
        {

            SoftUpdate app = new SoftUpdate(Application.ExecutablePath, "1");

            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);

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

 

    }

}

转载于:https://www.cnblogs.com/wangyhua/archive/2012/06/30/4050642.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值