当前位置: 移动技术网 > IT编程>开发语言>c# > C# Winform 自动更新程序实例详解

C# Winform 自动更新程序实例详解

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例为大家分享了c# winform 自动更新程序,供大家参考,具体内容如下 第一步:检查更新 检查更新其实无非就是去比较更新包的版本和本地软件版本,如果高则更新、

本文实例为大家分享了c# winform 自动更新程序,供大家参考,具体内容如下

第一步:检查更新

检查更新其实无非就是去比较更新包的版本和本地软件版本,如果高则更新、低则不更新。怎么获取版本号方法很多,本案例是获取软件的配置文件。

private bool checkupdate()
    {
      bool result = false;
      try
      {
        string cfg = txtread(exepath  "\\config.txt");
        configlocal = jsonconvert.deserializeobject<dto_config>(cfg);
 
        checkupdateurl = configlocal.autoupdateurl;
 
        cfg = txtread(checkupdateurl  "\\config.txt");
        configremote = jsonconvert.deserializeobject<dto_config>(cfg);        
 
        versionr = configremote.version;
        versionl = configlocal.version;
        int versionremote = int.parse(configremote.version.replace(".", ""));
        int versionlocal = int.parse(configlocal.version.replace(".", ""));
 
        result = versionremote > versionlocal;
      }
      catch { }
      return result;
    }

第二步:下载更新包

因为c/s的软件更新是面对所有用户,s端除了给c端提供基本的服务外,还可以给c端提供更新包。而这个s端可以是网络上的一个固定地址,也可以是局域网内一个公共盘。那下载更新包无非就是去访问服务端的文件,然后copy下来或下载下来。下面给出访问网络和访问局域网两个案例:

a、访问远程网络地址这里采用的是webclient

public void downloadfile()
    {
      if (!directory.exists(updatefiles))
      {
        directory.createdirectory(updatefiles);
      }
      using (webclient webclient = new webclient())
      {
        try
        {
          webclient.downloadfilecompleted = new asynccompletedeventhandler(client_downloadfilecompleted);
          webclient.downloadprogresschanged = new downloadprogresschangedeventhandler(client_downloadprogresschanged);
          webclient.downloadfileasync(new uri(checkupdateurl  "\\updatefile.rar"), updatefiles  "\\updatefile.rar");
        }
        catch (webexception ex)
        {
          messagebox.show(ex.message, "系统提示", messageboxbuttons.ok, messageboxicon.error);
        }
      }
    }

这里面应用到两个方法,downloadprogresschanged,监听异步下载的进度;downloadfilecompleted,监听完成异步文件下载;

private void client_downloadprogresschanged(object sender, downloadprogresschangedeventargs e)
    {
      this.progressbarupdate.minimum = 0;
      this.progressbarupdate.maximum = (int)e.totalbytestoreceive;
      this.progressbarupdate.value = (int)e.bytesreceived;
      this.lblpercent.text = e.progresspercentage  "%";
    }
private void client_downloadfilecompleted(object sender, asynccompletedeventargs e)
    {
      if (e.error != null)
      {
        messagebox.show(e.error.message, "系统提示", messageboxbuttons.ok, messageboxicon.error);
      }
      else
      {
        this.lblmessage.text = "下载完成";
        //复制更新文件替换旧文件
        directoryinfo thefolder = new directoryinfo(updatefiles);
        foreach (fileinfo nextfile in thefolder.getfiles())
        {
          file.copy(nextfile.fullname, application.startuppath  nextfile.name, true);
        }
        
      }
    }

b、访问服务端公共盘,直接采用file.copy

public void getremotefile()
    {
      try
      {
        directoryinfo thefolder = new directoryinfo(checkupdateurl);
        fileinfo[] filelist = thefolder.getfiles();
        this.progressbarupdate.minimum = 0;
        this.progressbarupdate.maximum = filelist.length;
 
        foreach (fileinfo nextfile in filelist)
        {
          if (nextfile.name != "config.txt")
          {
            file.copy(nextfile.fullname, exepath  "\\"  nextfile.name, true);            
          }
          this.lblmessage.text = "更新"  nextfile.name;
          this.progressbarupdate.value = 1;
          this.lblpercent.text = "更新进度... "  (this.progressbarupdate.value / filelist.length) * 100  "%";
        }
        this.lblmessage.text = "更新完成";
        //更改本地版本号为最新版本号
        configlocal.version = versionr;
        string cfgs = jsonconvert.serializeobject(configlocal);
        txtwrite(application.startuppath  "\\config.txt", cfgs);
      }
      catch (exception ex)
      {
        messagebox.show(ex.message, "系统提示", messageboxbuttons.ok, messageboxicon.error);
      }
    }

第三步:替换本地文件

这一步或许在第二步中已经实现了,如果你采用的是file.copy。替换也就是复制粘贴的问题。采用webclient下载了zip包,那还需解压一下压缩包然后再file.copy。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网