如何制作自动更新程序?

如何制作自动更新程序?

[版权所有 邱秋 2014 metaphysis@yeah.net, 转载请注明出处]

最近为单位写了一个C/S结构的软件,这个软件是工作在单位的局域网内的。为了减轻为程序进行升级的工作量,需要解决程序自动更新的问题。那么如何做一个自动更新程序呢?

想了一下,更新程序需要解决以下问题:

(A)它需要知道哪些是需要更新的文件,哪些是不需要的文件;

(B)它需要知道从哪里下载更新文件;

(C) 它需要将更新的文件下载下来,并将旧的文件替换掉,将不再需要的文件删除掉;

(D)它需要能够在更新完毕后自动重新启动程序以便用户继续使用;

问题(A)可以通过版本控制的方法来解决。具体方法是为程序所使用的文件都设定一个版本号,所有文件的版本号都记录在一个 XML 文件中,当升级时,检查最新程序的版本控制文件和当前的版本控制文件,当版本号较大时,则表示该文件需要更新。最新的版本控制文件可以放在一个匿名 FTP 上以便程序下载下来和本地的版本控制文件进行比对。如果一个文件不再需要了,则将该文件的版本信息从最新的版本控制文件中删除,通过对比控制文件,就知道该文件不再需要了,可以将之删除。由于我写的程序除主程序外,其他组件都不会发生太多改动,所以我使用了如下的方式来表示一个文件的版本信息:

[vb]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <AutoUpdater>  
  3.   <Updater>  
  4.     <UpdateUrl>ftp://192.168.1.24/update/</UpdateUrl>  
  5.     <MainVersion>1.1.102.0</MainVersion>  
  6.     <LastUpdateTime>2014-01-27</LastUpdateTime>  
  7.     <UpdateDescription>自动更新程序</UpdateDescription>  
  8.   </Updater>  
  9.   <UpdateFileList>  
  10.     <UpdateFile Version="2.2.5.0" Name="AForge.dll" />  
  11.     <UpdateFile Version="2.2.5.0" Name="AForge.Video.DirectShow.dll" />  
  12.     <UpdateFile Version="2.2.5.0" Name="AForge.Video.dll" />  
  13.     <UpdateFile Version="1.0.100.0" Name="USBCleaner.exe" />  
  14.     <UpdateFile Version="1.0.100.0" Name="USBViewer.exe" />  
  15.   </UpdateFileList>  
  16. </AutoUpdater>  

UpdateUrl 告诉程序要从什么地方下载最新的版本控制文件和更新文件,这里我使用了 FTP 的方式,这样简单一些,我将版本控制文件和最新的程序文件都放在了 ftp://192.168.1.24/update/ 下。MainVersion 表示程序的版本,用来确定是否需要进行升级。LastUpdateTime 表示程序最后的更新时间。UpdateDescription 表示更新程序的描述。UpdateFile 则表示程序中的每一个文件条目,Version 表示其版本,Name 表示相对于程序根目录的文件路径名,如果文件是在根目录下面,则直接是文件名,如果是在子目录下,则在前面加上相应的子目录。

有了这个版本控制文件,问题(B)也解决了,因为从指定的地址下载即可。

问题(C)可以通过比对版本控制文件,确定需要下载的文件和不再需要的文件。然后通过 WebClient 类来下载需要的文件。

问题(D)可以这样解决,主程序先检查是否有升级,如果有升级,则将旧的更新程序再复制一份,启动复制的更新程序,并启动它来下载更新文件,这样的话,就可以解决更新更新程序本身的问题,因为将新的更新程序下载来后,可以直接覆盖掉原始的更新程序而不会产生文件正在使用无法更新的问题,因为运行的是旧的更新程序的副本,在全部更新完毕后,主程序中可以加一段代码检测是否有更新副本产生,只要有就将它删除即可。

想清楚了这些问题,就是具体代码实现了,以下把版本文件解析的代码和更新下载文件的代码贴出来,整个更新模块也提供了下载,供有兴趣的朋友参考使用。下载链接:http://download.csdn.net/detail/metaphysis/6891593

[vb]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. XmlVersionConfigFile.vb  
  2.   
  3. Imports System.Xml  
  4. Imports System.Xml.Linq  
  5.   
  6. Public Class XmlVersionConfigFile  
  7.   
  8.     Public Property UpdateUrl As String = String.Empty  
  9.     Public Property MainVersion As Version = Version.Parse("0.0.0.0")  
  10.     Public Property LastUpdateTime As Date = DateTimePicker.MinimumDateTime  
  11.     Public Property UpdateDescription As String = String.Empty  
  12.     Public Property UpdateFileList As Dictionary(Of String, Version) = Nothing  
  13.   
  14.     Public Sub New(ByVal fileContent As String)  
  15.         ParseXmlVersionFile(fileContent)  
  16.     End Sub  
  17.   
  18.     Private Function ParseXmlVersionFile(ByVal fileContent As StringAs Boolean  
  19.         Dim xdoc As XDocument = Nothing  
  20.         Try  
  21.             xdoc = XDocument.Parse(fileContent)  
  22.         Catch ex As Exception  
  23.             Return False  
  24.         End Try  
  25.   
  26.         Me.UpdateUrl = xdoc.Element("AutoUpdater").Element("Updater").Element("UpdateUrl").Value  
  27.         Me.MainVersion = Version.Parse(xdoc.Element("AutoUpdater").Element("Updater").Element("MainVersion").Value)  
  28.         Date.TryParse(xdoc.Element("AutoUpdater").Element("Updater").Element("LastUpdateTime").Value, Me.LastUpdateTime)  
  29.         Me.UpdateDescription = xdoc.Element("AutoUpdater").Element("Updater").Element("UpdateDescription").Value  
  30.   
  31.         Me.UpdateFileList = New Dictionary(Of String, Version)  
  32.         Dim query = From UpdateFile In xdoc.Descendants("UpdateFile"Select UpdateFile  
  33.         For Each fileInfo As XElement In query  
  34.             Me.UpdateFileList.Add(fileInfo.Attribute("Name").Value.ToLower, Version.Parse(fileInfo.Attribute("Version").Value))  
  35.         Next  
  36.   
  37.         Return True  
  38.     End Function  
  39. End Class  
[vb]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. UpdatingForm.vb  
  2.   
  3. Imports System.IO  
  4.   
  5. Public Class UpdatingForm  
  6.   
  7.     Public Property LocalVersionConfig As XmlVersionConfigFile = Nothing  
  8.     Public Property ServerVersionConfig As XmlVersionConfigFile = Nothing  
  9.   
  10.     Private WithEvents webClient As New System.Net.WebClient  
  11.   
  12.     Private _downloadIndex As Integer  
  13.     Private _localConfigFileName As String = "version.xml"  
  14.     Private _localXmlFilePath As String = Path.Combine(Application.StartupPath, _localConfigFileName)  
  15.     Private _updateUrl As String = String.Empty  
  16.     Private _deleteFileList As New List(Of String)  
  17.   
  18.     Private Sub webClient_DownloadFileCompleted(ByVal sender As ObjectByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles webClient.DownloadFileCompleted  
  19.         Me.lvwFile.Items(_downloadIndex).ImageIndex = 2  
  20.   
  21.         lblSinglePercent.Text = "0%"  
  22.         prbSingle.Value = 0  
  23.   
  24.         DownloadNextFile()  
  25.     End Sub  
  26.   
  27.     Private Sub webClient_DownloadProgressChanged(ByVal sender As System.ObjectByVal e As System.Net.DownloadProgressChangedEventArgs) Handles webClient.DownloadProgressChanged  
  28.         Dim currentPercent As String = e.ProgressPercentage & "%"  
  29.         If currentPercent <> Me.lvwFile.Items(_downloadIndex).SubItems(3).Text Then  
  30.             Me.lvwFile.Items(_downloadIndex).SubItems(3).Text = currentPercent  
  31.             prbSingle.Value = e.ProgressPercentage  
  32.             lblSinglePercent.Text = currentPercent  
  33.             prbAll.Value = Int((_downloadIndex + 1) / Me.lvwFile.Items.Count * 100)  
  34.             lblAllPercent.Text = prbAll.Value & "%"  
  35.         End If  
  36.     End Sub  
  37.   
  38.     Private Sub btnQuit_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles btnQuit.Click  
  39.         Me.Close()  
  40.     End Sub  
  41.   
  42.     Private Sub DownloadNextFile()  
  43.         If _downloadIndex < (lvwFile.Items.Count - 1) Then  
  44.   
  45.             _downloadIndex += 1  
  46.   
  47.             lvwFile.Items(_downloadIndex).ImageIndex = 1  
  48.   
  49.             Try  
  50.                 Dim destPath As String = IO.Path.Combine(Application.StartupPath, lvwFile.Items(_downloadIndex).SubItems(1).Text)  
  51.                 File.Delete(destPath)  
  52.                 webClient.DownloadFileAsync(New Uri(_updateUrl & lvwFile.Items(_downloadIndex).SubItems(1).Text), destPath)  
  53.             Catch ex As Exception  
  54.                 Me.lvwFile.Items(_downloadIndex).ImageIndex = 3  
  55.                 MsgBox("下载文件发生错误,更新失败。错误原因: " & ex.Message, MsgBoxStyle.Critical, "错误")  
  56.                 Me.Close()  
  57.             End Try  
  58.         Else  
  59.             UpdateFileCompleted()  
  60.         End If  
  61.     End Sub  
  62.   
  63.     Private Sub UpdateFileCompleted()  
  64.         ' 更新显示信息。  
  65.         prbSingle.Value = prbSingle.Maximum  
  66.         lblSinglePercent.Text = "100%"  
  67.         lblAllPercent.Text = "100%"  
  68.   
  69.         ' 删除不需要的文件。  
  70.         For Each f As String In _deleteFileList  
  71.             Try  
  72.                 File.Delete(Path.Combine(Application.StartupPath, f))  
  73.             Catch ex As Exception  
  74.                 '  
  75.             End Try  
  76.         Next  
  77.   
  78.         Me.btnQuit.Enabled = True  
  79.         Process.Start(IO.Path.Combine(Application.StartupPath, "szpt.exe"))  
  80.         Me.Close()  
  81.     End Sub  
  82.   
  83.     Private Sub LoadUpdateFile()  
  84.         _updateUrl = Me.ServerVersionConfig.UpdateUrl  
  85.   
  86.         ' 查找客户端需要更新的文件和需要删除的文件。  
  87.         For Each p As KeyValuePair(Of String, Version) In Me.LocalVersionConfig.UpdateFileList  
  88.             If Me.ServerVersionConfig.UpdateFileList.ContainsKey(p.Key) Then  
  89.                 If Me.ServerVersionConfig.UpdateFileList(p.Key) > Me.LocalVersionConfig.UpdateFileList(p.Key) Then  
  90.                     Dim item As ListViewItem = Me.lvwFile.Items.Add(String.Empty, 0)  
  91.                     item.SubItems.Add(p.Key)  
  92.                     item.SubItems.Add(Me.ServerVersionConfig.UpdateFileList(p.Key).ToString)  
  93.                     item.SubItems.Add(String.Empty)  
  94.                 End If  
  95.             Else  
  96.                 _deleteFileList.Add(p.Key)  
  97.             End If  
  98.         Next  
  99.   
  100.         ' 查找服务器端新增需要下载的文件。  
  101.         For Each p As KeyValuePair(Of String, Version) In Me.ServerVersionConfig.UpdateFileList  
  102.             If Me.LocalVersionConfig.UpdateFileList.ContainsKey(p.Key) = False Then  
  103.                 Dim item As ListViewItem = Me.lvwFile.Items.Add(String.Empty, 0)  
  104.                 item.SubItems.Add(p.Key)  
  105.                 item.SubItems.Add(p.Value.ToString)  
  106.                 item.SubItems.Add(String.Empty)  
  107.             End If  
  108.         Next  
  109.   
  110.         ' 版本控制文件为必须下载文件。  
  111.         Dim itemVersion As ListViewItem = Me.lvwFile.Items.Add(String.Empty, 0)  
  112.         itemVersion.SubItems.Add("version.xml")  
  113.         itemVersion.SubItems.Add(Me.ServerVersionConfig.MainVersion.ToString)  
  114.         itemVersion.SubItems.Add(String.Empty)  
  115.   
  116.         ' 设置当前下载的文件序数。  
  117.         _downloadIndex = -1  
  118.     End Sub  
  119.   
  120.     Private Sub UpdatingForm_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
  121.         LoadUpdateFile()  
  122.         DownloadNextFile()  
  123.     End Sub  
  124.   
  125. End Class  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值