小白在线

爱生活 爱小白

C#实现程序的版本升级更新

我们做了程序,不免会有版本升级,这就需要程序有自动版本升级的功能。

那么看看我是如何实现程序自动更新的。

直接上代码:

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 = "http://www.csdn.net/update.xml";//升级配置的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> %3B 
        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) + "\\Update_" + Path.GetFileNameWithoutExtension(loadFile) + exten;  
                wc.DownloadFile(download, filename);  
                wc.Dispose();  
                isFinish();  
            }  
            catch  
            {  
                throw new Exception("更新出现错误,网络连接失败!");  
            }  
        }  
  
        /// <summary>  
        /// 检查是否需要更新  
        
<< 1 >>
«    2022年12月    »
1234
567891011
12131415161718
19202122232425
262728293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
    文章归档
    网站收藏
    友情链接
    • Z-BlogPHP
    • 订阅本站的 RSS 2.0 新闻聚合

    Powered By Z-BlogPHP 1.7.3

    Copyright iXiaoBai.net Rights Reserved. 吉ICP备18001369号-1 吉公网安备22010202001107