C#编程之C#实现程序的版本升级更新
小标 2018-11-14 来源 : 阅读 2835 评论 0

摘要:本文主要向大家介绍了C#编程之C#实现程序的版本升级更新,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

本文主要向大家介绍了C#编程之C#实现程序的版本升级更新,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

我们做了程序,不免会有版本升级,这就需要程序有自动版本升级的功能。那么看看我是如何实现程序自动更新的。
直接上代码:

using System;  
using System.Collections.Generic;  
using System.Text;  
using System.Reflection;  
using System.IO;  
using System.NET;  
using System.Xml;  
  
namespace Update  
{  
    /// 

  
    /// 更新完成触发的事件  
    /// 

  
    public delegate void UpdateState();  
    /// 

  
    /// 程序更新  
    /// 

  
    public class SoftUpdate  
    {  
  
        private string download;  
        private const string updateUrl = "//www.csdn.Net/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;  
  
        /// 

  
        /// 或取是否需要更新  
        /// 

  
        public bool IsUpdate  
        {  
            get   
            {  
                checkUpdate();  
                return isUpdate;   
            }  
        }  
  
        /// 

  
        /// 要检查更新的文件  
        /// 

  
        public string LoadFile  
        {  
            get { return loadFile; }  
            set { loadFile = value; }  
        }  
  
        /// 

  
        /// 程序集新版本  
        /// 

  
        public string NewVerson  
        {  
            get { return newVerson; }  
        }  
  
        /// 

  
        /// 升级的名称  
        /// 

  
        public string SoftName  
        {  
            get { return softName; }  
            set { softName = value; }  
        }  
 
        #endregion  
  
        /// 

  
        /// 更新完成时触发的事件  
        /// 

  
        public event UpdateState UpdateFinish;  
        private void isFinish() {  
            if(UpdateFinish != null)  
                UpdateFinish();  
        }  
  
        /// 

  
        /// 下载更新  
        /// 

  
        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) + "//Update_" + Path.GetFileNameWithoutExtension(loadFile) + exten;  
                wc.DownloadFile(download, filename);  
                wc.Dispose();  
                isFinish();  
            }  
            catch  
            {  
                throw new Exception("更新出现错误,网络连接失败!");  
            }  
        }  
  
        /// 

  
        /// 检查是否需要更新  
        /// 

  
        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("更新出现错误,请确认网络连接无误后重试!");  
            }  
        }  
  
        /// 

  
        /// 获取要更新的文件  
        /// 

  
        /// 

  
        public override string ToString()  
        {  
            return this.loadFile;  
        }  
    }  
}  

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

   

  
     
     1.0.1.2   
     //www.csdn.net/BlogWrite.rar   
  
  



程序更新调用方法:
 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 UpdateTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkUpdate();
        }

        public void checkUpdate()
        {
            SoftUpdate app = new SoftUpdate(Application.ExecutablePath, "BlogWriter");
            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);
        }

    }
}

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程