zoukankan      html  css  js  c++  java
  • .net winform软件自动更新

    转载自 http://dotnet.chinaitlab.com/DotNetFramework/914178.html

    关于.NET windows软件实现自动更新,本人今天写了一个DEMO,供大家参考。

            大家先看下效果图:

               

           主要涉及到两个方面

                1. 更新软件主项目和DLL文件

                2.升级包自身的更新

              

             1.   一个项目通常包括主项目和类库项目,主项目就是启动项目,以.exe结尾,类库项目主要是DLL, 简单的说更新就是将软件本地的主项目和类库项目进行更新。

       可以采用将最新的软件放到一个远程服务器上,然后每次启动本地软件时候,检查如果有更新,就从服务器下载最新的.EXE文件和DLL文件,

       来替换本地的DLL文件和exe文件

            实现思路:在本地和服务器上各放一个XML文件,里面记录了软件版本号,发布日期,要更新的DLL等一些信息,如果发现本地软件的版本号和服务器上的不相等,或者

           本地软件中的类库项目的发布时间比服务上的晚,就开始下载服务器上的文件,替换掉本地的文件。

            XML格式如下

      <?xml version="1.0" encoding="utf-8"?>
      <AutoUpdater>
      <AppName>WinUpdate</AppName>
      <ReleaseURL>http://127.0.0.1/webdown/</ReleaseURL>
      <ReleaseDate>2012/3/1 10:42:34</ReleaseDate>
      <ReleaseVersion>1.0.1.99</ReleaseVersion>
      <MinVersion>1.0.1.88</MinVersion>
      <UpdateDes>
        1、 添加打印菜单
        2、 增加DLL
        3、增加关于模块
      </UpdateDes>
      <ApplicationStart>WinUpdate.exe</ApplicationStart>
      <ShortcutIcon>ico</ShortcutIcon>
      <Releases>
        <File name="AboutForm.dll" date="2012/2/21 10:07:31" size="39" />
      </Releases>
        </AutoUpdater>

                   

             public static void DownloadFile(string localFolder, string remoteFolder, string fileName, ProgressBar bar,
                                            Label lblSize)
            {
                 string url = remoteFolder + "/" + fileName;
                string path = localFolder+ fileName;
                string dir = Path.GetDirectoryName(path);
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);
                WebRequest req = WebRequest.Create(url);
                WebResponse res = req.GetResponse();
                if (res.ContentLength == 0)
                    return;
                long fileLength = res.ContentLength;
                string totalSize = FormatFileSizeDescription(bar.Maximum);
                using (Stream srm = res.GetResponseStream())
                {
                    var srmReader = new StreamReader(srm);
                    var bufferbyte = new byte[fileLength];
                    int allByte = bufferbyte.Length;
                    int startByte = 0;
                    while (fileLength > 0)
                    {
                        int downByte = srm.Read(bufferbyte, startByte, allByte);
                        if (downByte == 0)
                        {
                            break;
                        }
                        ;
                        startByte += downByte;
                        allByte -= downByte;
                        int progress = bar.Value + downByte;
                        progress = progress > bar.Maximum ? bar.Maximum : progress;
                        bar.Value = progress;
                        lblSize.Text = string.Format("已完成{0}/{1}", FormatFileSizeDescription(progress), totalSize);
                    }
                    var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                    fs.Write(bufferbyte, 0, bufferbyte.Length);
                    srm.Close();
                    srmReader.Close();
                    fs.Close();
                }
            }
        }

    2.关于升级包自身的更新,采用如下思路,在服务器上放置一个TXT文件,里面存放着升级包的版本号,每次本地软件启动的时候,

        读取服务器上TXT文件的版本号和本地升级包的版本信息进行比较,如果不同,就从服务器上下载升级包。

           关于下载本项目都是使用WebClient进行完成的。

            自己可以采用如下方式进行测试

           首先,在你的IIS下面建立一个虚拟目录:http://127.0.0.1/webdown ,此目录用来放置要更新的文件,内容如下

              

             1.ReleaseList.xml和1.0.4.98文件夹主要是实现软件更新

                 ReleaseList.xml存放了需要更新的内容。1.0.4.98文件夹存放了需要更新的类库和文件

            2.  AutoUpdate.exe,UpdaterVerson.txt这两个文件实现的升级包自身进行更新.

                 AutoUpdate.exe是升级包,UpdaterVerson.txt存放的是升级包的版本号

           将以上内容部署到IIS下面

  • 相关阅读:
    记录一下idea自动生成Entity
    Spring-boot之 swagger2
    Spring-boot之 rabbitmq
    Js 跳出两级循环的方法
    Activiti 工作流变量的修改方法
    Spring-boot(二)yml文件的使用
    Spring-boot初始化创建(一)
    数据库性能优化:程序操作优化
    数据库性能优化:数据库表优化
    数据库性能优化:数据库自身优化
  • 原文地址:https://www.cnblogs.com/YangBinChina/p/3544348.html
Copyright © 2011-2022 走看看