.exe更新demo

来源:互联网 发布:c linux ide工具 编辑:程序博客网 时间:2024/05/22 02:50

百度网盘源码加文件:http://pan.baidu.com/s/1qYe2Vgg

功能:通过网站更新用户的软件,需要联网,也可以通过本地网站更新局域网用户软件。

根本实现:1.一个网站(本地就可以)然后运行update.exe->通过update.xml获取网址,然后查看当前版本号和网站的server.xml最高版本号对比。然后判断是否更新。

        2.更新就下载zip文件,解压替换并删除。  

        3.更新update.xml文档版本信息,跟新结束。

代码:解析xml文件:

复制代码
     /// <summary>        /// 载入并解析XML        /// </summary>        private void LoadXml()        {            XmlDocument document = new XmlDocument();            try            {                document.Load(updateurl + "Server.xml");                XmlNode node = document.SelectSingleNode("//updates");                //获取最新版本号                version = Convert.ToInt32(node.Attributes["version"].Value);                //所有需要升级文件大小,文件列表,sql列表                XmlNodeList updateList = document.SelectNodes("//updates//update");                foreach (XmlNode n in updateList)                {                    long tempVersion = Convert.ToInt32(n.Attributes["version"].Value);                    long tempSize = Convert.ToInt64(n.Attributes["size"].Value);                    if (tempVersion > localversion && tempVersion <= version)                    {                        allsize += tempSize;                        versions.Add(tempVersion.ToString());                        //获取升级文件列表                        XmlNodeList fileList = n.SelectNodes("//update[@version='" + tempVersion + "']//files//file");                        foreach (XmlNode n1 in fileList)                        {                            files.Add(n1.InnerText);                        }                        //获取执行的SQL语句列表                        XmlNodeList sqlList = n.SelectNodes("//update[@version='" + tempVersion + "']//sqls//sql");                        foreach (XmlNode n2 in sqlList)                        {                            sqls.Add(n2.InnerText);                        }                        //升级的提示信息                        XmlNodeList msgList = n.SelectNodes("//update[@version='" + tempVersion + "']//msg");                        foreach (XmlNode n3 in msgList)                        {                            msg += string.Format(CultureInfo.InvariantCulture, "版本【{0}】 {1}", new object[] { tempVersion, n3.InnerText.Replace("\r\n\t\t", "") }) + "\r\n";                        }                    }                }            }            catch (Exception e)            {                //Console.WriteLine(e.Message);                MessageBox.Show("连接升级服务器失败,请重试!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);            }        }
复制代码
复制代码
复制代码
 /// <summary>        /// 开始升级        /// </summary>        private void StartUpdate()        {            if (localversion >= version)            {                UpdateCompleted(0);                return;            }            this.downWebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged);            this.downWebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);            if (this.allsize == 0L || files == null || files.Count == 0)            {                this.UpdateCompleted(0);            }            if (files != null && files.Count > 0)            {                DownloadFile(0);            }        }
复制代码

 

复制代码
复制代码
   public void DownloadProgressChanged(object wcsender, DownloadProgressChangedEventArgs ex)        {            PBNow.Text = "正在下载:" + filename + "[ " + ConvertSize(ex.BytesReceived) + "/" + ConvertSize(ex.TotalBytesToReceive) + " ]";            filesize = ex.TotalBytesToReceive;            ftemp = ((float)(downedsize + ex.BytesReceived)) / ((float)allsize);            PbAll.Value = Convert.ToInt32((float)(this.ftemp * 100f));            PBNow.Value = ex.ProgressPercentage;        }
复制代码

     跟新效果:

更新前:

更新后:

一些代码图解:本地update.xml

网站server.xml

新手:代码粗糙,只是一个简单的demo,只能简单参考一下,有问题及时联系我。源码和全部文件都在网盘里面。

0 1
原创粉丝点击