×

用VB.NET做winform的在线更新程序

Kalet Kalet 发表于2009-03-20 12:00:14 浏览182 评论0

抢沙发发表评论

因为项目需要做了一个在线升级程序,非常爽,所以弄出来和大家分享一下。
可能到时候项目做大了,我会做断点续传与多任务多线程进去,当然有兴趣的朋友可以共同探讨一下
主要用到类库中的webrequest,webresponse和webclient类 用VB.NET做winform的在线更新程序
另外使用到了devexpress进度条控件

有兴趣的朋友可加我QQ270499458 雨浪
俺大学学的政治法律,转行到计算机有一年多了,很多地方还不足,希望高手赐教
大家一起研究.net

整个程序分为3个部分
1.客户机上的配置文件
2.服务器上的配置文件与需要更新的文件
3.客户机上的升级程序


首先介绍一下客户机上的配置文件,相关部分我会详细说明
文件名称 update.ini
文件内容
[serverpath]
address=http://www.mycom.com/update/
主要就是说明一下要获取升级文件的地址
请一定要在最后加上"/",因为我懒,在程序中没有判断是否有"/"

服务器上的配置文件
文件名称 update.xml
文件内容
<?xml version="1.0" encoding="gb2312"?>
<update>
<file>
<filename>要升级的文件名称</filename>
<version>文件的版本</version>
<lastmodifydate>文件最后修改的日期</lastmodifydate>
<filelength>文件的大小(单位为字节)</filelength>
<target>文件对于客户机运行目录下的相对地址</target>
<downurl>升级文件存放的url地址</downurl>
</file>
</update>
这里我举例说明一下用法,假如我有两个文件要升级
<?xml version="1.0" encoding="gb2312"?>
<update>
<file>
<filename>我的管理系统.exe</filename>
<version>1.0.2207.21855</version>
<lastmodifydate>2006-1-16</lastmodifydate>
<filelength>7401472</filelength>
<target>\我的管理系统.exe</target>
<downurl>http://www.mycom.com/update/main.exe</downurl>
</file>
<file>
<filename>email.dll</filename>
<version>1.0.2207.21855</version>
<lastmodifydate>2006-1-16</lastmodifydate>
<filelength>90112</filelength>
<target>\email.dll</target>
<downurl>http://www.mycom.com/update/email.dll</downurl>
</file>
</update>
我做的这个升级程序的运行机制是这样的:
在LOAD事件里面创建一个临时文件夹,放下载的文件的
(1)首先下载UPDATE.XML
(2)将XML内容读到dataset中去
(3)判断客户机上是否有这些文件,如果没有,就是要更新的
(4)如果客户机上有这些文件,如果版本的那一列也就是version不为空的话,将与客户机进行版本比对
(5)如果版本那一列为空,将进行最后修改日期比对,因为有些更新的不是有版本的文件,比如文本文件,就需要进行最后修改日期比对
(6)如果版本或者修改日期都一样,就不需要更新,从dataset中删除那些不需要更新的文件行
(7)下载文件
(8)杀掉运行的主程序进程
(9)将要更新的文件复制到目标处
(10)删除临时文件夹的所有内容
(11)运行主程序
WEBASP这个发表文档不能发图片,好郁闷呀~~~~~

再说说重点,客户机上的升级程序,我把所有程序都贴在里面了。大家研究研究,如果照搬是不能运行的,稍微修改就可以了,因为我引用了我自己做的几个类,没有也不需要贴出来,还有用到了DEVEXPRESS控件,不过要点都注释了。
具体代码如下:
Imports System
Imports System.Net
Imports System.Text
Imports System.Threading
Imports commanfunc
Imports Scripting
Imports System.io
Imports api


Friend Class update
Inherits System.Windows.Forms.Form

Private mywebclient As New WebClient
Private bytenum As Long
Private threaddown, threadchk As Thread
Private mybyte() As Byte
Private zipsize As Long
Private func As New func
Private fso As New FileSystemObject
Private fl As Scripting.File
Private ready As String = Application.StartupPath & "\images\ready.ico"
Private down As String = Application.StartupPath & "\images\down.ico"
Private inipath As String = Application.StartupPath & "\update.ini"
Private tempfolder As String = Application.StartupPath & "\temp_download" '临时下载文件夹
Private address As String '下载网站

Private updatedataset As New DataSet '升级配置文件数据集
Private curversion As String
Private alldownloadbyte As Long = 0
Private runthread As Thread '执行时候运行的线程

Private myapi As New api.myAPI


#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
Friend WithEvents PictureBox3 As System.Windows.Forms.PictureBox
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents lbl As System.Windows.Forms.Label
Friend WithEvents btnupdate As System.Windows.Forms.Button
Friend WithEvents btnclose As System.Windows.Forms.Button
Friend WithEvents Label8 As System.Windows.Forms.Label
Friend WithEvents Label9 As System.Windows.Forms.Label
Friend WithEvents Label10 As System.Windows.Forms.Label
Friend WithEvents PictureBox4 As System.Windows.Forms.PictureBox
Friend WithEvents PictureBox9 As System.Windows.Forms.PictureBox
Friend WithEvents Progressdownload As DevExpress.XtraEditors.ProgressBarControl
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents ProgressCdownload As DevExpress.XtraEditors.ProgressBarControl
Friend WithEvents Label7 As System.Windows.Forms.Label
Friend WithEvents PictureBox5 As System.Windows.Forms.PictureBox
Friend WithEvents PictureBox6 As System.Windows.Forms.PictureBox
Friend WithEvents PictureBox7 As System.Windows.Forms.PictureBox
Friend WithEvents PictureBox8 As System.Windows.Forms.PictureBox
Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
Friend WithEvents Label3 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(update))
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Label1 = New System.Windows.Forms.Label
Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
Me.Label2 = New System.Windows.Forms.Label
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.ProgressCdownload = New DevExpress.XtraEditors.ProgressBarControl
Me.Label4 = New System.Windows.Forms.Label
Me.Progressdownload = New DevExpress.XtraEditors.ProgressBarControl
Me.Label5 = New System.Windows.Forms.Label
Me.PictureBox2 = New System.Windows.Forms.PictureBox
Me.PictureBox3 = New System.Windows.Forms.PictureBox
Me.PictureBox4 = New System.Windows.Forms.PictureBox
Me.Label6 = New System.Windows.Forms.Label
Me.btnupdate = New System.Windows.Forms.Button
Me.lbl = New System.Windows.Forms.Label
Me.btnclose = New System.Windows.Forms.Button
Me.PictureBox5 = New System.Windows.Forms.PictureBox
Me.Label8 = New System.Windows.Forms.Label
Me.PictureBox6 = New System.Windows.Forms.PictureBox
Me.Label9 = New System.Windows.Forms.Label
Me.PictureBox7 = New System.Windows.Forms.PictureBox
Me.Label10 = New System.Windows.Forms.Label
Me.PictureBox9 = New System.Windows.Forms.PictureBox
Me.PictureBox8 = New System.Windows.Forms.PictureBox
Me.Label7 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.GroupBox1.SuspendLayout()
CType(Me.ProgressCdownload.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.Progressdownload.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.Location = New System.Drawing.Point(16, 72)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(24, 24)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'Label1
'
Me.Label1.ImageList = Me.ImageList1
Me.Label1.Location = New System.Drawing.Point(64, 80)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(128, 16)
Me.Label1.TabIndex = 1
Me.Label1.Text = "正在连接到服务器..."
'
'ImageList1
'
Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)
Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(64, 112)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(128, 16)
Me.Label2.TabIndex = 3
Me.Label2.Text = "正在检查可更新版本..."
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.ProgressCdownload)
Me.GroupBox1.Controls.Add(Me.Label4)
Me.GroupBox1.Controls.Add(Me.Progressdownload)
Me.GroupBox1.Controls.Add(Me.Label5)
Me.GroupBox1.Location = New System.Drawing.Point(8, 216)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(456, 104)
Me.GroupBox1.TabIndex = 14
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "下载进度"
'
'ProgressCdownload
'
Me.ProgressCdownload.AccessibleName = "custom"
Me.ProgressCdownload.Location = New System.Drawing.Point(80, 32)
Me.ProgressCdownload.Name = "ProgressCdownload"
'
'ProgressCdownload.Properties
'
Me.ProgressCdownload.Properties.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.HotFlat
Me.ProgressCdownload.Properties.EndColor = System.Drawing.Color.Cornsilk
Me.ProgressCdownload.Properties.LookAndFeel.UseDefaultLookAndFeel = False
Me.ProgressCdownload.Properties.LookAndFeel.UseWindowsXPTheme = False
Me.ProgressCdownload.Properties.ProgressViewStyle = DevExpress.XtraEditors.Controls.ProgressViewStyle.Solid
Me.ProgressCdownload.Properties.ShowTitle = True
Me.ProgressCdownload.Properties.StartColor = System.Drawing.Color.BurlyWood
Me.ProgressCdownload.Size = New System.Drawing.Size(352, 21)
Me.ProgressCdownload.TabIndex = 6
Me.ProgressCdownload.TabStop = False
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(16, 37)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(64, 16)
Me.Label4.TabIndex = 5
Me.Label4.Text = "当前进度"
'
'Progressdownload
'
Me.Progressdownload.AccessibleName = "custom"
Me.Progressdownload.Location = New System.Drawing.Point(80, 64)
Me.Progressdownload.Name = "Progressdownload"
'
'Progressdownload.Properties
'
Me.Progressdownload.Properties.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.HotFlat
Me.Progressdownload.Properties.EndColor = System.Drawing.Color.Cornsilk
Me.Progressdownload.Properties.LookAndFeel.UseDefaultLookAndFeel = False
Me.Progressdownload.Properties.LookAndFeel.UseWindowsXPTheme = False
Me.Progressdownload.Properties.ProgressViewStyle = DevExpress.XtraEditors.Controls.ProgressViewStyle.Solid
Me.Progressdownload.Properties.ShowTitle = True
Me.Progressdownload.Properties.StartColor = System.Drawing.Color.BurlyWood
Me.Progressdownload.Size = New System.Drawing.Size(352, 21)
Me.Progressdownload.TabIndex = 4
Me.Progressdownload.TabStop = False
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(16, 69)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(64, 16)
Me.Label5.TabIndex = 1
Me.Label5.Text = "总体进度"
'
'PictureBox2
'
Me.PictureBox2.Location = New System.Drawing.Point(16, 104)
Me.PictureBox2.Name = "PictureBox2"
Me.PictureBox2.Size = New System.Drawing.Size(24, 24)
Me.PictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox2.TabIndex = 15
Me.PictureBox2.TabStop = False
'
'PictureBox3
'
Me.PictureBox3.Location = New System.Drawing.Point(16, 136)
Me.PictureBox3.Name = "PictureBox3"
Me.PictureBox3.Size = New System.Drawing.Size(24, 24)
Me.PictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox3.TabIndex = 16
Me.PictureBox3.TabStop = False
'
'PictureBox4
'
Me.PictureBox4.Location = New System.Drawing.Point(16, 168)
Me.PictureBox4.Name = "PictureBox4"
Me.PictureBox4.Size = New System.Drawing.Size(24, 24)
Me.PictureBox4.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox4.TabIndex = 18
Me.PictureBox4.TabStop = False
'
'Label6
'
Me.Label6.Location = New System.Drawing.Point(64, 176)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(128, 16)
Me.Label6.TabIndex = 17
Me.Label6.Text = "正在下载更新版本..."
'
'btnupdate
'
Me.btnupdate.Location = New System.Drawing.Point(280, 336)
Me.btnupdate.Name = "btnupdate"
Me.btnupdate.Size = New System.Drawing.Size(96, 24)
Me.btnupdate.TabIndex = 19
Me.btnupdate.Text = "开始升级"
'
'lbl
'
Me.lbl.ForeColor = System.Drawing.Color.Red
Me.lbl.Location = New System.Drawing.Point(16, 344)
Me.lbl.Name = "lbl"
Me.lbl.Size = New System.Drawing.Size(248, 16)
Me.lbl.TabIndex = 20
'
'btnclose
'
Me.btnclose.Location = New System.Drawing.Point(384, 336)
Me.btnclose.Name = "btnclose"
Me.btnclose.Size = New System.Drawing.Size(80, 24)
Me.btnclose.TabIndex = 21
Me.btnclose.Text = "关闭"
'
'PictureBox5
'
Me.PictureBox5.Location = New System.Drawing.Point(224, 72)
Me.PictureBox5.Name = "PictureBox5"
Me.PictureBox5.Size = New System.Drawing.Size(24, 24)
Me.PictureBox5.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox5.TabIndex = 25
Me.PictureBox5.TabStop = False
'
'Label8
'
Me.Label8.Location = New System.Drawing.Point(272, 80)
Me.Label8.Name = "Label8"
Me.Label8.Size = New System.Drawing.Size(136, 16)
Me.Label8.TabIndex = 24
Me.Label8.Text = "正在关闭应用程序..."
'
'PictureBox6
'
Me.PictureBox6.Location = New System.Drawing.Point(224, 104)
Me.PictureBox6.Name = "PictureBox6"
Me.PictureBox6.Size = New System.Drawing.Size(24, 24)
Me.PictureBox6.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox6.TabIndex = 27
Me.PictureBox6.TabStop = False
'
'Label9
'
Me.Label9.Location = New System.Drawing.Point(272, 112) 用VB.NET做winform的在线更新程序
Me.Label9.Name = "Label9"
Me.Label9.Size = New System.Drawing.Size(136, 16)
Me.Label9.TabIndex = 26
Me.Label9.Text = "正在执行更新过程..."
'
'PictureBox7
'
Me.PictureBox7.Location = New System.Drawing.Point(224, 136)
Me.PictureBox7.Name = "PictureBox7"
Me.PictureBox7.Size = New System.Drawing.Size(24, 24)
Me.PictureBox7.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox7.TabIndex = 29
Me.PictureBox7.TabStop = False
'
'Label10
'
Me.Label10.Location = New System.Drawing.Point(272, 144)
Me.Label10.Name = "Label10"
Me.Label10.Size = New System.Drawing.Size(136, 16)
Me.Label10.TabIndex = 28
Me.Label10.Text = "更新完成,删除临时文件"
'
'PictureBox9
'
Me.PictureBox9.BackColor = System.Drawing.SystemColors.HighlightText
Me.PictureBox9.Dock = System.Windows.Forms.DockStyle.Top
Me.PictureBox9.Image = CType(resources.GetObject("PictureBox9.Image"), System.Drawing.Image)
Me.PictureBox9.Location = New System.Drawing.Point(0, 0)
Me.PictureBox9.Name = "PictureBox9"
Me.PictureBox9.Size = New System.Drawing.Size(474, 58)
Me.PictureBox9.TabIndex = 30
Me.PictureBox9.TabStop = False
'
'PictureBox8
'
Me.PictureBox8.Location = New System.Drawing.Point(224, 168)
Me.PictureBox8.Name = "PictureBox8"
Me.PictureBox8.Size = New System.Drawing.Size(24, 24)
Me.PictureBox8.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox8.TabIndex = 32
Me.PictureBox8.TabStop = False
'
'Label7
'
Me.Label7.Location = New System.Drawing.Point(272, 176)
Me.Label7.Name = "Label7"
Me.Label7.Size = New System.Drawing.Size(136, 16)
Me.Label7.TabIndex = 31
Me.Label7.Text = "更新成功,开启应用程序"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(64, 144)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(128, 16)
Me.Label3.TabIndex = 5
Me.Label3.Text = "正在收集本机信息..."
'
'update
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(474, 375)
Me.Controls.Add(Me.PictureBox8)
Me.Controls.Add(Me.Label7)
Me.Controls.Add(Me.PictureBox9)
Me.Controls.Add(Me.PictureBox7)
Me.Controls.Add(Me.Label10)
Me.Controls.Add(Me.PictureBox6)
Me.Controls.Add(Me.Label9)
Me.Controls.Add(Me.PictureBox5)
Me.Controls.Add(Me.Label8)
Me.Controls.Add(Me.btnclose)
Me.Controls.Add(Me.lbl)
Me.Controls.Add(Me.btnupdate)
Me.Controls.Add(Me.PictureBox4)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.PictureBox3)
Me.Controls.Add(Me.PictureBox2)
Me.Controls.Add(Me.GroupBox1)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.PictureBox1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "update"
Me.ShowInTaskbar = False
Me.Text = "在线升级"
Me.TopMost = True
Me.GroupBox1.ResumeLayout(False)
CType(Me.ProgressCdownload.Properties, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.Progressdownload.Properties, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

'载入
Private Sub update_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myapi.DisableX(Me) '让关闭X不可用
reset()
getaddress()
End Sub

'还原状态
Private Sub reset()
PictureBox1.Image = Image.FromFile(ready)
PictureBox2.Image = Image.FromFile(ready)
PictureBox3.Image = Image.FromFile(ready)
PictureBox4.Image = Image.FromFile(ready)
PictureBox5.Image = Image.FromFile(ready)
PictureBox6.Image = Image.FromFile(ready)
PictureBox7.Image = Image.FromFile(ready)
PictureBox8.Image = Image.FromFile(ready)
'lbl.Text = ""
'btnupdate.Enabled = True
'btnclose.Enabled = True
End Sub

'让btn可用
Private Sub resetbtn()
btnupdate.Enabled = True
btnclose.Enabled = True
End Sub

'检测文件夹是否存在,不存在则建立
Private Sub chkexsitfolder(ByVal foldername As String)
If fso.FolderExists(foldername) = False Then
fso.CreateFolder(foldername)
End If
End Sub

'检测文件是否存在,返回boolean值
Private Function chkexsitfile(ByVal filename As String) As Boolean
Return fso.FileExists(filename)
End Function

'获取文件版本信息 获取成功返回版本值,不成功返回nothing
Private Function getcurversion(ByVal filepath As String) As String
Try
curversion = FileVersionInfo.GetVersionInfo(filepath).FileVersion.ToString
Return curversion
Catch ex As Exception
Return Nothing
End Try
End Function

'获取配置文件中的地址 地址最后必须加上"/"
Private Sub getaddress()
Try
address = func.GetKeyVal(inipath, "serverpath", "address")
Catch ex As Exception
lbl.Text = "连接服务器失败..."
resetbtn()
Exit Sub
End Try
End Sub

'测试连接到服务器 并下载升级文件
Private Sub connectsvr()
PictureBox1.Image = Image.FromFile(down)
Try
If fso.FolderExists(tempfolder) = False Then '如果不存在临时文件夹子,则先建立
fso.CreateFolder(tempfolder)
End If
mywebclient.DownloadFile(address & "update.xml", tempfolder & "\update.xml")
Catch ex As Exception
lbl.Text = "连接服务器失败..."
resetbtn()
Exit Sub
End Try
End Sub

'检查更新版本 读取数据集 如果数据集为空则失败
Private Sub getnewversion()
PictureBox2.Image = Image.FromFile(down)
Try
updatedataset.ReadXml(tempfolder & "\update.xml")
If updatedataset.Tables(0).Rows.Count <= 0 Or (updatedataset Is Nothing) Then
lbl.Text = "检查可更新版本失败..."
reset()
End If
Catch ex As Exception
lbl.Text = "检查可更新版本失败..."
resetbtn()
Exit Sub
End Try
End Sub


'分析更新版本 并获取所有要下载的字节数
Private Sub chkupdate()
PictureBox3.Image = Image.FromFile(down)
alldownloadbyte = 0
chkupdatemethod()
If updatedataset.Tables("file").Rows.Count = 0 Then
lbl.Text = "您目前的版本已经是最新版..."
resetbtn()
Exit Sub
End If
End Sub

'分析更新版本具体方法,采用了递归
Private Sub chkupdatemethod()
Dim i As Integer
Try
For i = 0 To updatedataset.Tables(0).Rows.Count - 1
'分析存在性,如果不存在则是需要升级的,如果存在分析版本 如果版本为空则分析最后修改时间
If chkexsitfile(Application.StartupPath & updatedataset.Tables("file").Rows(i)("target")) = True Then
'如果版本号相等则删除此行
If func.cdbnull(updatedataset.Tables("file").Rows(i)("version")) <> "" Then
If getcurversion(Application.StartupPath & updatedataset.Tables("file").Rows(i)("target")) = updatedataset.Tables("file").Rows(i)("version") Then
updatedataset.Tables("file").Rows.Item(i).Delete()
chkupdatemethod()
Exit Sub
Else
alldownloadbyte += updatedataset.Tables("file").Rows(i)("filelength")
End If
Else
'如果修改时间大于或者相等则删除此行
fl = fso.GetFile(Application.StartupPath & updatedataset.Tables("file").Rows(i)("target"))
If fl.DateLastModified >= CType(updatedataset.Tables("file").Rows(i)("lastmodifydate"), Date) Then
updatedataset.Tables("file").Rows.Item(i).Delete()
chkupdatemethod()
Exit Sub
Else
alldownloadbyte += updatedataset.Tables("file").Rows(i)("filelength")
End If
End If
Else
alldownloadbyte += updatedataset.Tables("file").Rows(i)("filelength")
End If
Next
Catch ex As Exception
lbl.Text = "升级失败,无法分析更新版本..."
resetbtn()
Exit Sub
End Try
End Sub

Private Sub downfiles()
connectsvr()
If btnupdate.Enabled = True Then
Exit Sub
End If
getnewversion()
If btnupdate.Enabled = True Then
Exit Sub
End If
chkupdate()
If btnupdate.Enabled = True Then
Exit Sub
End If
Dim i As Integer
Dim srm As Stream
Dim mbyte() As Byte
Dim allbyte As Long
Dim startbyte As Integer
Dim m As Integer
Dim fs As FileStream
Dim myre As HttpWebRequest
Dim mwrite As HttpWebResponse
Dim wc As WebClient = New WebClient
PictureBox4.Image = Image.FromFile(down)
Progressdownload.Position = 0
Progressdownload.Properties.Maximum = alldownloadbyte
Try
For i = 0 To updatedataset.Tables("file").Rows.Count - 1
GroupBox1.Text = "下载进度(" & (i + 1).ToString & "/" & updatedataset.Tables("file").Rows.Count & ")"
ProgressCdownload.Position = 0 '设置当前进度为0
startbyte = 0 '开始下载的位置为0
ReDim mbyte(CLng(updatedataset.Tables("file").Rows(i)("filelength"))) '本也可以直接获取文件大小,但是很占用资源,干脆写在配置文件内
myre = CType(WebRequest.Create(updatedataset.Tables("file").Rows(i)("downurl")), HttpWebRequest)
mwrite = CType(myre.GetResponse(), HttpWebResponse)
srm = wc.OpenRead(updatedataset.Tables("file").Rows(i)("downurl"))
allbyte = mbyte.Length
ProgressCdownload.Properties.Maximum = allbyte
ProgressCdownload.Position = 0
Do While updatedataset.Tables("file").Rows(i)("filelength") > 0
m = srm.Read(mbyte, startbyte, allbyte)
If m = 0 Then Exit Do
startbyte += m
allbyte -= m
ProgressCdownload.Position += m
Progressdownload.Position += m
Loop

fs = New FileStream(tempfolder & "\" & updatedataset.Tables("file").Rows(i)("filename"), FileMode.OpenOrCreate)
fs.Write(mbyte, 0, mbyte.Length)
fs.Flush()
fs.Close()
myre.Abort() '这里必须释放资源,否则下载多个文件出现连接超时错误
srm.Close()
runthread.Sleep(2000) '这里每下一个文件让线程等待2秒,太快可能服务器没有响应
Next
Progressdownload.Position = alldownloadbyte '防止人为写错字节数不到100的现象
Catch ex As Exception
lbl.Text = "下载更新文件失败..."
srm.Close()
myre.Abort()
resetbtn()
Exit Sub
End Try
closeexe()
updatefile()
deltmpfiles()
If btnupdate.Enabled = True Then
Exit Sub
End If
startexe()
End Sub


'关闭应用程序
Private Sub closeexe()
PictureBox5.Image = Image.FromFile(down)
func.killprogress("我的管理系统") '这里是我引用的一个类,用来杀进程的
End Sub

'更新要升级的文件
Private Sub updatefile()
PictureBox6.Image = Image.FromFile(down)
Try
Dim i As Integer
Dim flcopy As IO.File
For i = 0 To updatedataset.Tables("file").Rows.Count - 1
flcopy.Copy(tempfolder & "\" & updatedataset.Tables("file").Rows(i)("filename"), Application.StartupPath & updatedataset.Tables("file").Rows(i)("target"), True)
Next
Catch ex As Exception
lbl.Text = "升级到新版本失败,可能应用程序未关闭..."
resetbtn()
Exit Sub
End Try
End Sub

'删除文件
Private Sub deltmpfiles()
PictureBox7.Image = Image.FromFile(down)
Try
fso.DeleteFolder(tempfolder, True)
Catch ex As Exception
Exit Sub
End Try
End Sub

'启动应用程序
Private Sub startexe()
PictureBox8.Image = Image.FromFile(down)
Try
System.Diagnostics.Process.Start(Application.StartupPath & "\我的管理系统.exe")
Catch ex As Exception
lbl.Text = "更新成功,但未能启动应用程序,请手动启动..."
Finally
btnclose.Enabled = True
End Try
End Sub


'升级
Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
reset()
btnupdate.Enabled = False
btnclose.Enabled = False
GroupBox1.Text = "下载进度"
lbl.Text = ""
updatedataset.Clear()
runthread = New Thread(AddressOf downfiles) '不知道为什么,用了JOIN后将会出现卡屏,用线程池\完成事件\轮循都不行,我只有把其他事件放在这个线程里了。郁闷
runthread.Start()
'runthread.Join()
End Sub

Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
Me.Close()
End Sub
End Class

用VB.NET做winform的在线更新程序
这个升级程序运行的比较稳定,但是还有很多不足,特别对于线程的控制,很难把握。希望高手不另赐教~~~


群贤毕至

访客