织梦做网站首页,东莞ui设计公司,承德市隆化城乡建设局网站,建个公司网站一年多少钱原文出自#xff1a;http://blog.csdn.net/metaphysis/article/details/18866631 如何制作自动更新程序#xff1f; [版权所有 邱秋 2014 metaphysisyeah.net, 转载请注明出处] 最近为单位写了一个C/S结构的软件#xff0c;这个软件是工作在单位的局域网内的。为了减轻为程序…原文出自http://blog.csdn.net/metaphysis/article/details/18866631 如何制作自动更新程序 [版权所有 邱秋 2014 metaphysisyeah.net, 转载请注明出处] 最近为单位写了一个C/S结构的软件这个软件是工作在单位的局域网内的。为了减轻为程序进行升级的工作量需要解决程序自动更新的问题。那么如何做一个自动更新程序呢 想了一下更新程序需要解决以下问题 A它需要知道哪些是需要更新的文件哪些是不需要的文件 B它需要知道从哪里下载更新文件 C 它需要将更新的文件下载下来并将旧的文件替换掉将不再需要的文件删除掉 D它需要能够在更新完毕后自动重新启动程序以便用户继续使用 问题A可以通过版本控制的方法来解决。具体方法是为程序所使用的文件都设定一个版本号所有文件的版本号都记录在一个 XML 文件中当升级时检查最新程序的版本控制文件和当前的版本控制文件当版本号较大时则表示该文件需要更新。最新的版本控制文件可以放在一个匿名 FTP 上以便程序下载下来和本地的版本控制文件进行比对。如果一个文件不再需要了则将该文件的版本信息从最新的版本控制文件中删除通过对比控制文件就知道该文件不再需要了可以将之删除。由于我写的程序除主程序外其他组件都不会发生太多改动所以我使用了如下的方式来表示一个文件的版本信息 [vb] view plaincopy ?xml version1.0 encodingutf-8? AutoUpdater Updater UpdateUrlftp://192.168.1.24/update//UpdateUrl MainVersion1.1.102.0/MainVersion LastUpdateTime2014-01-27/LastUpdateTime UpdateDescription自动更新程序/UpdateDescription /Updater UpdateFileList UpdateFile Version2.2.5.0 NameAForge.dll / UpdateFile Version2.2.5.0 NameAForge.Video.DirectShow.dll / UpdateFile Version2.2.5.0 NameAForge.Video.dll / UpdateFile Version1.0.100.0 NameUSBCleaner.exe / UpdateFile Version1.0.100.0 NameUSBViewer.exe / /UpdateFileList /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 plaincopy XmlVersionConfigFile.vb Imports System.Xml Imports System.Xml.Linq Public Class XmlVersionConfigFile Public Property UpdateUrl As String String.Empty Public Property MainVersion As Version Version.Parse(0.0.0.0) Public Property LastUpdateTime As Date DateTimePicker.MinimumDateTime Public Property UpdateDescription As String String.Empty Public Property UpdateFileList As Dictionary(Of String, Version) Nothing Public Sub New(ByVal fileContent As String) ParseXmlVersionFile(fileContent) End Sub Private Function ParseXmlVersionFile(ByVal fileContent As String) As Boolean Dim xdoc As XDocument Nothing Try xdoc XDocument.Parse(fileContent) Catch ex As Exception Return False End Try Me.UpdateUrl xdoc.Element(AutoUpdater).Element(Updater).Element(UpdateUrl).Value Me.MainVersion Version.Parse(xdoc.Element(AutoUpdater).Element(Updater).Element(MainVersion).Value) Date.TryParse(xdoc.Element(AutoUpdater).Element(Updater).Element(LastUpdateTime).Value, Me.LastUpdateTime) Me.UpdateDescription xdoc.Element(AutoUpdater).Element(Updater).Element(UpdateDescription).Value Me.UpdateFileList New Dictionary(Of String, Version) Dim query From UpdateFile In xdoc.Descendants(UpdateFile) Select UpdateFile For Each fileInfo As XElement In query Me.UpdateFileList.Add(fileInfo.Attribute(Name).Value.ToLower, Version.Parse(fileInfo.Attribute(Version).Value)) Next Return True End Function End Class [vb] view plaincopy UpdatingForm.vb Imports System.IO Public Class UpdatingForm Public Property LocalVersionConfig As XmlVersionConfigFile Nothing Public Property ServerVersionConfig As XmlVersionConfigFile Nothing Private WithEvents webClient As New System.Net.WebClient Private _downloadIndex As Integer Private _localConfigFileName As String version.xml Private _localXmlFilePath As String Path.Combine(Application.StartupPath, _localConfigFileName) Private _updateUrl As String String.Empty Private _deleteFileList As New List(Of String) Private Sub webClient_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles webClient.DownloadFileCompleted Me.lvwFile.Items(_downloadIndex).ImageIndex 2 lblSinglePercent.Text 0% prbSingle.Value 0 DownloadNextFile() End Sub Private Sub webClient_DownloadProgressChanged(ByVal sender As System.Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles webClient.DownloadProgressChanged Dim currentPercent As String e.ProgressPercentage % If currentPercent Me.lvwFile.Items(_downloadIndex).SubItems(3).Text Then Me.lvwFile.Items(_downloadIndex).SubItems(3).Text currentPercent prbSingle.Value e.ProgressPercentage lblSinglePercent.Text currentPercent prbAll.Value Int((_downloadIndex 1) / Me.lvwFile.Items.Count * 100) lblAllPercent.Text prbAll.Value % End If End Sub Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click Me.Close() End Sub Private Sub DownloadNextFile() If _downloadIndex (lvwFile.Items.Count - 1) Then _downloadIndex 1 lvwFile.Items(_downloadIndex).ImageIndex 1 Try Dim destPath As String IO.Path.Combine(Application.StartupPath, lvwFile.Items(_downloadIndex).SubItems(1).Text) File.Delete(destPath) webClient.DownloadFileAsync(New Uri(_updateUrl lvwFile.Items(_downloadIndex).SubItems(1).Text), destPath) Catch ex As Exception Me.lvwFile.Items(_downloadIndex).ImageIndex 3 MsgBox(下载文件发生错误更新失败。错误原因 ex.Message, MsgBoxStyle.Critical, 错误) Me.Close() End Try Else UpdateFileCompleted() End If End Sub Private Sub UpdateFileCompleted() 更新显示信息。 prbSingle.Value prbSingle.Maximum lblSinglePercent.Text 100% lblAllPercent.Text 100% 删除不需要的文件。 For Each f As String In _deleteFileList Try File.Delete(Path.Combine(Application.StartupPath, f)) Catch ex As Exception End Try Next Me.btnQuit.Enabled True Process.Start(IO.Path.Combine(Application.StartupPath, szpt.exe)) Me.Close() End Sub Private Sub LoadUpdateFile() _updateUrl Me.ServerVersionConfig.UpdateUrl 查找客户端需要更新的文件和需要删除的文件。 For Each p As KeyValuePair(Of String, Version) In Me.LocalVersionConfig.UpdateFileList If Me.ServerVersionConfig.UpdateFileList.ContainsKey(p.Key) Then If Me.ServerVersionConfig.UpdateFileList(p.Key) Me.LocalVersionConfig.UpdateFileList(p.Key) Then Dim item As ListViewItem Me.lvwFile.Items.Add(String.Empty, 0) item.SubItems.Add(p.Key) item.SubItems.Add(Me.ServerVersionConfig.UpdateFileList(p.Key).ToString) item.SubItems.Add(String.Empty) End If Else _deleteFileList.Add(p.Key) End If Next 查找服务器端新增需要下载的文件。 For Each p As KeyValuePair(Of String, Version) In Me.ServerVersionConfig.UpdateFileList If Me.LocalVersionConfig.UpdateFileList.ContainsKey(p.Key) False Then Dim item As ListViewItem Me.lvwFile.Items.Add(String.Empty, 0) item.SubItems.Add(p.Key) item.SubItems.Add(p.Value.ToString) item.SubItems.Add(String.Empty) End If Next 版本控制文件为必须下载文件。 Dim itemVersion As ListViewItem Me.lvwFile.Items.Add(String.Empty, 0) itemVersion.SubItems.Add(version.xml) itemVersion.SubItems.Add(Me.ServerVersionConfig.MainVersion.ToString) itemVersion.SubItems.Add(String.Empty) 设置当前下载的文件序数。 _downloadIndex -1 End Sub Private Sub UpdatingForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load LoadUpdateFile() DownloadNextFile() End Sub End Class