欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现在线更新软件

程序员文章站 2022-06-03 18:53:57
通过某些手段后台更新软件。首先你要有一个放置新版本信息的网站 updatesoftwareform.cs using system; usin...

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

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

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