C#实现在线更新软件

所属分类: 软件编程 / C#教程 阅读数: 34
收藏 0 赞 0 分享

通过某些手段后台更新软件。首先你要有一个放置新版本信息的网站

UpdateSoftwareForm.cs

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
 
    #region 获取版本号
 
    /// <summary>
    /// 从服务器上获取最新的版本号
    /// </summary>
    public void DownloadCheckUpdateXml()
    {
      try
      {
        //第一个参数是文件的地址,第二个参数是文件保存的路径文件名
        client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WriteBook2.xml", "WriteBook2.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("WriteBook2.xml.xml"))
        {
          XmlDocument doc = new XmlDocument();
          //加载要读取的XML
          doc.Load("WriteBook2.xml.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 初始化程序
 
    /// <summary>
    /// 初始化程序
    /// </summary>
    private void InitializeandInstall()
    {
      UpdateProgressBar.Value = 20;
      DownloadCheckUpdateXml();
      UpdateProgressBar.Value = 40;
      NowVersion();
      UpdateProgressBar.Value = 60;
      LatestVersion();
      UpdateProgressBar.Value = 80;
      DownloadInstall();
      UpdateProgressBar.Value = 100;
    }
 
    #endregion
 
    #region 安装and删除
 
    /// <summary>
    /// 下载安装包
    /// </summary>
    public void DownloadInstall()
    {
      try
      {
        if (nowversion == latesversion)
        {
          MessageBox.Show("您已经是最新版本。", "提示");
        }
        else if (nowversion != latesversion && File.Exists("WriteBook2.xml"))
        {
          MessageBox.Show("发现新版本,即将下载更新补丁。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
          client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WBsetup.exe", "WBsetup.exe");
          if (File.Exists("Setup.exe"))
          {
            InstallandDelete();
          }
          else
          {
            for (int i = 1; i < 3; i++)
            {
              client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WBsetup.exe", "WBsetup.exe");
            }
            MessageBox.Show("下载失败,请检查您的网络连接是否正常。", "提示");
            this.Close();
          }
        }
      }
      catch
      {
        MessageBox.Show("更新失败,没有发现新版本。", "提示");
        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("WBsetup.exe");
          Thread td = new Thread(JudgeInstall);
          td.Start();
        }
        else { }
      }
      catch
      {
        MessageBox.Show("发生未知错误,更新失败。", "提示");
        this.Close();
      }
    }
 
    /// <summary>
    /// 判断安装进程是否存在
    /// </summary>
    public void JudgeInstall()
    {
      while (true)
      {
        Process[] processList = Process.GetProcesses();
        foreach (Process process in processList)
        {
          if (process.ProcessName == "WBsetup.exe") { }
          else
          {
            DialogResult dr = MessageBox.Show("更新成功,是否删除安装包?", "提示", MessageBoxButtons.YesNo);
            if (dr == System.Windows.Forms.DialogResult.Yes)
            {
              File.Delete("WBsetup.exe");
              File.Delete("WriteBook2.xml");
            }
          }
        }
      }
    }
 
    #endregion
 
    /// <summary>
    /// 点击初始化程序
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void UpdateButton_Click(object sender, EventArgs e)
    {
      InitializeandInstall();
    }
  }
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

更多精彩内容其他人还在看

winform用datagridview制作课程表实例

这篇文章主要介绍了winform用datagridview制作课程表的方法,实例分析了WinForm实现课程表的结构、数据库及调用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#中winform控制textbox输入只能为数字的方法

这篇文章主要介绍了C#中winform控制textbox输入只能为数字的方法,包括使用keyPress事件限制键盘输入以及TextChanged事件限制粘贴等情况,来实现控制输入为数字的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

C#省份城市下拉框联动简单实现方法

这篇文章主要介绍了C#省份城市下拉框联动简单实现方法,涉及字典的定义与索引的用法,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#处理MySql多个返回集的方法

这篇文章主要介绍了C#处理MySql多个返回集的方法,实现了对处理MySql多个返回集进行封装,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#无限参数的写法

这篇文章主要介绍了C#无限参数的写法,通过循环遍历再结合paras.Add方法实现无限参数的功能,是比较实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#反射应用实例

这篇文章主要介绍了C#反射应用,实例分析了通过反射实现多系统数据库的配置方法,是比较实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#窗体传值实例汇总

这篇文章主要介绍了C#窗体传值,实例形式汇总了静态变量传值、委托传值、对话框之间的传值等常见应用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#把数组中的某个元素取出来放到第一个位置的实现方法

这篇文章主要介绍了C#把数组中的某个元素取出来放到第一个位置的实现方法,涉及C#针对数组的常见操作技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#中Equality和Identity浅析

这篇文章主要介绍了C#中Equality和Identity浅析,本文先是讲解了Equality和Identity的定义,同时讲解了判断两个对象等价性的4种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

在Linux上运行C#的方法

这篇文章主要介绍了在Linux上运行C#的方法,实例分析了Linux平台下Mono软件包的应用技巧,以及在此基础之上的C#运行方法,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多