• 通过web对.exe程序进行更新和修改


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

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

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

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

    主要代码:

    1.解析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("
    		", "") }) + "
    ";
                            }
                        }
    
                    }
    
                }
                catch (Exception e)
                {
                    //Console.WriteLine(e.Message);
                    MessageBox.Show("连接升级服务器失败,请重试!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

    2.下载文件:

     private void DownloadFile(int index)
            {
                try
                {
                    downindex++;
                    filename = files[index];
                    //LabelNow.Text = "开始下载版本【" + versions[index] + "】" + filename;
                    LabelAll.Text = string.Format(CultureInfo.InvariantCulture, "升级进度 {0}/{1}  [ {2} ]", new object[] { downindex, files.Count, this.ConvertSize(allsize) });
                    PBNow.Value = 0;
                    downWebClient.DownloadFileAsync(new Uri(updateurl + filename), Application.StartupPath + "/temp/" + filename);
    
                }
                catch (WebException exception)
                {
                    MessageBox.Show(exception.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

    3.开始升级:

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

    4.下载完成并更改update.xml

      private void DownloadFileCompleted(object wcsender, AsyncCompletedEventArgs ex)
            {
                if (ex.Error != null)
                {
                    MessageBox.Show(ex.Error.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    if (File.Exists(Application.StartupPath + @"" + this.filename))
                    {
                        File.Delete(Application.StartupPath + @"" + this.filename);
                    }
                    File.Move(Application.StartupPath + @"	emp" + this.filename, Application.StartupPath + @"" + this.filename);
                    new FastZip().ExtractZip(Application.StartupPath + @"" + this.filename, Application.StartupPath + @"", "");
                    File.Delete(Application.StartupPath + @"" + this.filename);
                    this.downedsize += this.filesize;
                    if (this.files.Count > this.downindex)
                    {
                        this.DownloadFile(this.downindex);
                    }
                    else
                    {
                        XmlTextWriter writer = new XmlTextWriter(Application.StartupPath + @"update.xml", null)
                        {
                            Formatting = Formatting.Indented,
                            Indentation = 4
                        };
                        writer.WriteStartDocument();
                        writer.WriteStartElement("update");
                        writer.WriteStartElement("ProcessName");
                        writer.WriteString(this.ProcessName);
                        writer.WriteEndElement();
                        writer.WriteStartElement("version");
                        writer.WriteString(this.version.ToString());
                        writer.WriteEndElement();
                        writer.WriteStartElement("url");
                        writer.WriteString(this.updateurl);
                        writer.WriteEndElement();
                        writer.WriteEndElement();
                        writer.WriteEndDocument();
                        writer.Flush();
                        writer.Close();
                        if (!string.IsNullOrEmpty(this.msg))
                        {
                            MessageBox.Show(this.msg, "升级提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        }
                        this.UpdateCompleted(1);
                    }
                }
            }

    跟新进度条效果图:

    更新前:

    更新后:

    一些文档代码图解:

    1.本地update.xml

    2.网站server.xml

    代码粗糙,只是一个简单的demo,简单参考一下,有错误及时联系我。

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

  • 相关阅读:
    window计时器函数
    日期函数
    字符的方法
    五大主流浏览器与四大内核
    向一个排好序的数组中插入一个数组。
    冒泡与排序
    数组去重方法1,2,3
    有执行语句:console.log(fn2(2)[3]),补充函数,使执行结果为"hello"
    面试题:一个函数的调用 fn(2)(3)(4) 得到的结果位24; 这个函数是柯里化函数运用了闭包原理
    python中append()与extend()方法的区别
  • 原文地址:https://www.cnblogs.com/guiqulaixici/p/6722616.html
Copyright © 2020-2023  润新知