65.9K
CodeProject 正在变化。 阅读更多。
Home

应用程序自动更新 Revisited

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.84/5 (22投票s)

2006年11月19日

CPOL

2分钟阅读

viewsIcon

242701

downloadIcon

6176

一篇关于通过网络更新 Windows 应用程序的文章。

引言

自从我的自动更新的第一次迭代以来,我注意到我实际上并不需要更改自动更新程序本身的能力,因此我不需要它。 另外,大多数情况下,我不需要更新所有文件。

变更内容

自动更新程序 (AutoUpdate.exe) 不再需要。 现在,所有必需的内容都在 DLL (AutoUpdate.dll) 中。 由于所有内容都位于同一位置,因此不再需要修改 CommandLineAutoUpdate 不需要是一个类,现在它只是一个模块,因此不需要创建 AutoUpdate 变量。

无需更改 AutoUpdate 代码。 远程路径、更新文件名和错误消息现在是属性,后两个属性具有默认值。

更新文件具有新的布局,如下所示

<File Name>;<Version>   [' comments    ]
<File Name>[;<Version>] [' comments    ]

<File Name>[;?]         [' comments    ]
<File Name>[;delete]    [' comments    ]
...

这意味着

  • 空行和注释将被忽略。
  • 第一行应该是当前程序/版本。
  • 从第二行到结尾,第二个参数是可选的。
  • 如果未指定第二个参数,则会更新文件。
  • 如果指定了版本,则更新会检查版本。
  • 如果第二个参数是问号 (?),则更新会检查文件是否已存在,如果存在则“不”升级。
  • 如果第二个参数是“delete”,则系统会尝试删除该文件。
  • “'” (chr(39)) 开始行注释(类似于 VB)。

UpdateFiles 函数返回 True 如果 AutoUpdate 执行了更新或更新过程中出现错误,或者返回 False 如果没有执行任何操作。

自动更新 Web 文件夹

有些事情永远不会改变。 自动更新 Web 文件夹应该为要升级的每个系统创建一个文件夹。 根文件夹是您将在 RemotePath 变量中引用的文件夹。 每个子文件夹应以程序集名称命名(通常,不带扩展名的程序名称)。 在程序文件夹内,您可以保存要更新的文件以及文件 Update.txt(或在 UpdateFileName 属性中定义的名称),其布局如上所述。

使用代码

您可以将模块添加到项目中,也可以添加对 DLL 的引用。 之后,您只需要调用 UpdateFiles 函数。 您也可以在调用之前更改默认属性。

Public Sub Main()
    ' You can set some parameters thru properties
    ' The remote path can be set thru the RemotePath property or 
    ' thru the RemotePath parameter in the function call
    ' UpdateFileName and ErrorMessages have default value so it's optional
    AutoUpdate.RemotePath = "http://www.url.com/directory/"

    ' the final location to the files will be RemotePath + AssembyName + "/" + FileName
    ' ex: http://www.url.com/directory/AutoUpdateTest/MyUpdate.dat

    AutoUpdate.UpdateFileName = "MyUpdate.dat"
    AutoUpdate.ErrorMessage = "Something funny is going on trying to auto update."

    ' test if an update is needed and quit the program if so.
    If AutoUpdate.UpdateFiles() Then Exit Sub

    ' here goes your regular code in the main sub
    Application.Run(New Form1)
End Sub

还能做什么

在服务器端,您可以构建一个自动生成更新文件的服务,但这取决于您!

© . All rights reserved.