野人 发表于 2015-7-12 00:18

C#实现在线更新软件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CCWin;
using System.Net;
using System.Collections;
using System.IO;
using System.Xml;
using System.Diagnostics;
using System.Threading;

namespace WriteBook
{
    public partial class UpdateSoftwareForm : Skin_Metro
    {
      public UpdateSoftwareForm()
      {
            InitializeComponent();
      }

      #region 一些对象和变量

      //使用WebClient下载
      WebClient client = new WebClient();
      ArrayList downlist = new ArrayList();
      //当前版本
      string nowversion = null;
      //最新版本
      string latesversion = null;

      #endregion

      /// <summary>
      /// 窗体运行时
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void UpdateSoftwareForm_Load(object sender, EventArgs e)
      {
            nowversion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + "\n";
            LocalText.Text = nowversion;
      }

      /// <summary>
      /// 点击初始化程序
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void UpdateButton_Click(object sender, EventArgs e)
      {
            InitializeandInstall();
      }

      /// <summary>
      /// 初始化程序
      /// </summary>
      private void InitializeandInstall()
      {
            skinProgressIndicator1.AutoStart = true;
            DownloadCheckUpdateXml();
            NowVersion();
            LatestVersion();
            DownloadInstall();
      }

      #region 获取版本号

      /// <summary>
      /// 从服务器上获取最新的版本号
      /// </summary>
      public void DownloadCheckUpdateXml()
      {
            try
            {
                //第一个参数是文件的地址,第二个参数是文件保存的路径文件名
                client.DownloadFile("http://cloudtours.net/web/WriteBook/WriteBook211.XML", @"Update\WriteBook211.XML");
            }
            catch
            {
                MessageBox.Show("没有检测到更新。", "提示");
                this.Close();
            }
      }

      /// <summary>
      /// 获取本地软件的版本号
      /// </summary>
      private void NowVersion()
      {
            nowversion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + "\n";
            LocalText.Text = nowversion;
      }

      /// <summary>
      /// 读取从服务器获取的最新版本号
      /// </summary>
      public void LatestVersion()
      {
            try
            {
                if (File.Exists(@"Update\WriteBook211.XML"))
                {
                  XmlDocument doc = new XmlDocument();
                  //加载要读取的XML
                  doc.Load(@"Update\WriteBook211.XML");

                  //获得根节点
                  XmlElement WriteBook = doc.DocumentElement;

                  //获得子节点 返回节点的集合
                  XmlNodeList Update = WriteBook.ChildNodes;

                  foreach (XmlNode item in Update)
                  {
                        latesversion = item.InnerText;
                  }
                  LatestText.Text = latesversion;
                }
                else
                {
                  MessageBox.Show("没有检测到更新。", "提示");
                  this.Close();
                }
            }
            catch
            {
                this.Close();
            }
      }

      #endregion

      #region 安装and删除

      /// <summary>
      /// 下载安装包
      /// </summary>
      public void DownloadInstall()
      {
            try
            {
                if (nowversion == latesversion)
                {
                  MessageBox.Show("您已经是最新版本。", "提示");
                }
                else if (nowversion != latesversion && File.Exists(@"Update\WriteBook211.XML"))
                {
                  MessageBox.Show("发现新版本,即将下载更新补丁。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                  client.DownloadFile("http://cloudtours.net/web/WriteBook/WBsetup2_0_2.exe", @"Update\WBsetup2_0_2.exe");
                  if (File.Exists(@"Update\WBsetup2_0_2.exe"))
                  {
                        InstallandDelete();
                  }
                  else
                  {
                        for (int i = 1; i < 3; i++)
                        {
                            client.DownloadFile("http://cloudtours.net/web/WriteBook/WBsetup2_0_2.exe", @"Update\WBsetup2_0_2.exe");
                        }
                        MessageBox.Show("下载失败,请检查您的网络连接是否正常。", "提示");
                        this.Close();
                  }
                }
            }
            catch
            {
                MessageBox.Show("更新失败,没有发现新版本。", "提示");
                File.Delete(@"Update\WBsetup2_0_2.exe");
                File.Delete(@"Update\WriteBook211.XML");
                this.Close();
            }
      }

      /// <summary>
      /// 安装及删除
      /// </summary>
      private void InstallandDelete()
      {
            try
            {
                DialogResult dr = MessageBox.Show("下载更新成功,是否安装新更新?", "提示", MessageBoxButtons.YesNoCancel);
                if (dr == System.Windows.Forms.DialogResult.Yes)
                {
                  //启动安装程序
                  System.Diagnostics.Process.Start(@"Update\WBsetup2_0_2.exe");
                  Thread td = new Thread(JudgeInstall);
                  td.Start();
                }
                else
                {
                  File.Delete(@"Update\WBsetup2_0_2.exe");
                  File.Delete(@"Update\WriteBook211.XML");
                }
            }
            catch
            {
                MessageBox.Show("发生未知错误,更新失败。", "提示");
                File.Delete(@"Update\WBsetup2_0_2.exe");
                File.Delete(@"Update\WriteBook211.XML");
                this.Close();
            }
      }

      /// <summary>
      /// 判断安装进程是否存在
      /// </summary>
      public void JudgeInstall()
      {
            Process[] processList = Process.GetProcesses();
            foreach (Process process in processList)
            {
                if (process.ProcessName == "WBsetup2_0_2.exe") { JudgeInstall(); }
                else
                {
                  while (true)
                  {
                        DialogResult dr = MessageBox.Show("更新成功,是否删除安装包?", "提示", MessageBoxButtons.YesNo);
                        if (dr == DialogResult.Yes)
                        {
                            File.Delete(@"Update\WBsetup2_0_2.exe");
                            File.Delete(@"Update\WriteBook211.XML");
                            return;
                        }
                        else { return; }
                  }
                }
            }
      }

      #endregion

    }
}


野人 发表于 2015-7-12 00:20

在线更新软件 - 开源中国社区 http://www.oschina.net/code/snippet_2368961_48115#69903

妠小小 发表于 2015-7-15 16:27

额               

余新港~1550750 发表于 2021-6-17 21:06

看着代码,我仿佛青春又回来了
页: [1]
查看完整版本: C#实现在线更新软件