声明:核心功能的实现是由园子里圣殿骑士大哥写的,本人是基于他核心代码,按照自己需求进行修改的。

     而AutoUpdaterService.xml文件生成工具是基于评论#215楼 ptangbao的代码而改写的。

由于这个组件是在10年写的,.net也有更新有的方法已提示过时,更改如下:

//Added the function to support proxy
//clientDownload.Proxy = System.Net.WebProxy.GetDefaultProxy();
clientDownload.Proxy = WebRequest.GetSystemWebProxy();

更改的主要功能如下:

  1》如果有更新将会直接更新,不再提供由用户点击确定后再更新。(强制更新)(这个暂时没有整理出来,后续会整理出来)

  2》更新前判断主程序进程是否开启:

  如果有更新,主程序开启,关闭主程序,更新完成后自动启动主程序。

  如果没有更新,直接启动主程序。

  3》不再根据版本号不同进行更新。

  圣殿骑士大哥的是根据版本号,当然这也是最正规的,可是我们的程序有点特殊,所以不再根据版本号控制,而是根据GUID。

  这样就是有一点好处不管版本号一不一样,只要GUID不一样就是要更新。

  比如文件夹如下:

  Winform(C#.NET)自动更新组件的使用及部分功能实现

  使用CreateXmlTools.exe工具生成xml文件,增加的节点属性有version,值时GUID

<?xml version="1.0" encoding="utf-8"?>
<updateFiles>
  <file path="AutoUpdater.dll" url="http://172.30.100.55:8011/AutoUpdater.dll" lastver="5.0.0.0" size="26624" needRestart="false" version="1ef2b9dc-d14f-4fc4-a5ec-bdb07a6ba98c" />
  <file path="ReadMe.dll" url="http://172.30.100.55:8011/ReadMe.dll" lastver="" size="472" needRestart="false" version="3ddc1926-3088-468f-9088-92b07156c757" />
  <file path="aspnet_client/ReadMe.dll" url="http://172.30.100.55:8011/aspnet_client/ReadMe.dll" lastver="" size="472" needRestart="false" version="4aaa87e2-63bd-486a-9957-1c2df21607cb" />
</updateFiles>

  version就是用来替代lastver的,只要不一样就更新

  4》客户端更新主程序更改autoupdater.config文件的更新方式

  config里不必包含所有文件的配置,只要求配置成如下:  

<?xml version="1.0" encoding="utf-8" ?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Enabled>true</Enabled>
  <ServerUrl>http://172.30.100.55:8011/AutoupdateService.xml</ServerUrl>
  <UpdateFileList>
    
  </UpdateFileList>
</Config>

  ServerUrl就是web服务器的地址加上面生成xml文件的地址。

  更新完成后客户端会自动更新autoupdater.config文件,将本地的guid保持与服务端一致,再次点击guid一致的不再更新

以上是大致改动的地方。

下面来说说代码吧:

由于增加了GUID这块,所以RemoteFile、LocalFile和DownloadFileInfo三个实体类都应该增加一个字段和一个属性

LocalFile.cs

 1     public class LocalFile
 2     {
 3         #region The private fields
 4         private string path = "";
 5         private string lastver = "";
 6         private int size = 0;
 7         private string version = "";
 8         #endregion
 9 
10         #region The public property
11         [XmlAttribute("path")]
12         public string Path { get { return path; } set { path = value; } }
13         [XmlAttribute("lastver")]
14         public string LastVer { get { return lastver; } set { lastver = value; } }
15         [XmlAttribute("size")]
16         public int Size { get { return size; } set { size = value; } }
17         [XmlAttribute("version")]
18         public string Version { get { return version; } set { version = value; } }
19         #endregion
20 
21         #region The constructor of LocalFile
22         public LocalFile(string path, string ver, int size,string versionid)
23         {
24             this.path = path;
25             this.lastver = ver;
26             this.size = size;
27             this.version = versionid;
28         }
29 
30         public LocalFile()
31         {
32         }
33         #endregion
34 
35     }
View Code

相关文章: