VB.NET实现应用程序自动更新2

作者在 2009-01-07 13:36:08 发布以下内容

Imports System
Imports System.Net
Imports System.Text
Imports System.Threading
Imports Scripting
Imports System.io
Imports Fairy4_Api
Imports DevExpress.XtraEditors
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 Err As String = Application.StartupPath & "\images\Error.ico"
    Private Inipath As String = Application.StartupPath & "\update.ini"
    Private Tempfolder As String = Application.StartupPath & "\temp_download" '临时下载文件夹
    Private Address As String '下载网站
    Private SleepTime As Integer '下載完一個進程休眠時間:單位毫秒
    Private RunProgrom As String
    Private KillProgrom As String

    Public UpdateDataSet As New DataSet '升级配置文件数据集
    Private Curversion As String
    Private Alldownloadbyte As Long = 0
    Private Runthread As Thread '执行时候运行的线程
    Private Myapi As New Fairy4_Api.My_Api
    '############################
    '程式製作:Fairy4
    '參考網友:雨浪 部分源碼
    'Mail_To: Phoenix4197@163.com
    '############################

#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 ' 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
    Friend WithEvents Progressdownload As DevExpress.XtraEditors.ProgressBarControl
    Friend WithEvents ProgressCdownload As DevExpress.XtraEditors.ProgressBarControl
    Friend WithEvents AxWebBrowser1 As AxSHDocVw.AxWebBrowser
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    <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.Progressdownload = New DevExpress.XtraEditors.ProgressBarControl
        Me.Label4 = New System.Windows.Forms.Label
        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.AxWebBrowser1 = New AxSHDocVw.AxWebBrowser
        Me.GroupBox2 = New System.Windows.Forms.GroupBox
        Me.GroupBox1.SuspendLayout()
        CType(Me.ProgressCdownload.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.Progressdownload.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.GroupBox2.SuspendLayout()
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.Location = New System.Drawing.Point(20, 83)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(20, 20)
        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(60, 86)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(123, 17)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "正在连接到服务器..."
        '
        'ImageList1
        '
        Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)
        Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(60, 120)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(123, 17)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "正在检查更新版本..."
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.ProgressCdownload)
        Me.GroupBox1.Controls.Add(Me.Progressdownload)
        Me.GroupBox1.Controls.Add(Me.Label4)
        Me.GroupBox1.Controls.Add(Me.Label5)
        Me.GroupBox1.Location = New System.Drawing.Point(15, 231)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(369, 112)
        Me.GroupBox1.TabIndex = 14
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "下载进度"
        '
        'ProgressCdownload
        '
        Me.ProgressCdownload.Location = New System.Drawing.Point(80, 40)
        Me.ProgressCdownload.Name = "ProgressCdownload"
        Me.ProgressCdownload.Size = New System.Drawing.Size(264, 14)
        Me.ProgressCdownload.TabIndex = 7
        Me.ProgressCdownload.TabStop = False
        '
        'Progressdownload
        '
        Me.Progressdownload.Location = New System.Drawing.Point(80, 72)
        Me.Progressdownload.Name = "Progressdownload"
        Me.Progressdownload.Size = New System.Drawing.Size(264, 14)
        Me.Progressdownload.TabIndex = 6
        Me.Progressdownload.TabStop = False
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(13, 40)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(54, 17)
        Me.Label4.TabIndex = 5
        Me.Label4.Text = "当前进度"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(13, 74)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(54, 17)
        Me.Label5.TabIndex = 1
        Me.Label5.Text = "总体进度"
        '
        'PictureBox2
        '
        Me.PictureBox2.Location = New System.Drawing.Point(20, 117)
        Me.PictureBox2.Name = "PictureBox2"
        Me.PictureBox2.Size = New System.Drawing.Size(20, 20)
        Me.PictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.PictureBox2.TabIndex = 15
        Me.PictureBox2.TabStop = False
        '
        'PictureBox3
        '
        Me.PictureBox3.Location = New System.Drawing.Point(20, 152)
        Me.PictureBox3.Name = "PictureBox3"
        Me.PictureBox3.Size = New System.Drawing.Size(20, 20)
        Me.PictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.PictureBox3.TabIndex = 16
        Me.PictureBox3.TabStop = False
        '
        'PictureBox4
        '
        Me.PictureBox4.Location = New System.Drawing.Point(20, 186)
        Me.PictureBox4.Name = "PictureBox4"
        Me.PictureBox4.Size = New System.Drawing.Size(20, 20)
        Me.PictureBox4.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.PictureBox4.TabIndex = 18
        Me.PictureBox4.TabStop = False
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(60, 189)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(123, 17)
        Me.Label6.TabIndex = 17
        Me.Label6.Text = "正在下载更新版本..."
        '
        'btnupdate
        '
        Me.btnupdate.Image = CType(resources.GetObject("btnupdate.Image"), System.Drawing.Image)
        Me.btnupdate.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.btnupdate.Location = New System.Drawing.Point(223, 360)
        Me.btnupdate.Name = "btnupdate"
        Me.btnupdate.Size = New System.Drawing.Size(81, 26)
        Me.btnupdate.TabIndex = 19
        Me.btnupdate.Text = "开始升级"
        Me.btnupdate.TextAlign = System.Drawing.ContentAlignment.BottomRight
        '
        'lbl
        '
        Me.lbl.ForeColor = System.Drawing.Color.Red
        Me.lbl.Location = New System.Drawing.Point(21, 361)
        Me.lbl.Name = "lbl"
        Me.lbl.Size = New System.Drawing.Size(181, 17)
        Me.lbl.TabIndex = 20
        '
        'btnclose
        '
        Me.btnclose.Image = CType(resources.GetObject("btnclose.Image"), System.Drawing.Image)
        Me.btnclose.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.btnclose.Location = New System.Drawing.Point(320, 360)
        Me.btnclose.Name = "btnclose"
        Me.btnclose.Size = New System.Drawing.Size(56, 26)
        Me.btnclose.TabIndex = 21
        Me.btnclose.Text = "关闭"
        Me.btnclose.TextAlign = System.Drawing.ContentAlignment.BottomRight
        '
        'PictureBox5
        '
        Me.PictureBox5.Location = New System.Drawing.Point(197, 83)
        Me.PictureBox5.Name = "PictureBox5"
        Me.PictureBox5.Size = New System.Drawing.Size(20, 20)
        Me.PictureBox5.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.PictureBox5.TabIndex = 25
        Me.PictureBox5.TabStop = False
        '
        'Label8
        '
        Me.Label8.Location = New System.Drawing.Point(237, 86)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(140, 17)
        Me.Label8.TabIndex = 24
        Me.Label8.Text = "正在关闭应用程序..."
        '
        'PictureBox6
        '
        Me.PictureBox6.Location = New System.Drawing.Point(197, 117)
        Me.PictureBox6.Name = "PictureBox6"
        Me.PictureBox6.Size = New System.Drawing.Size(20, 20)
        Me.PictureBox6.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.PictureBox6.TabIndex = 27
        Me.PictureBox6.TabStop = False
        '
        'Label9
        '
        Me.Label9.Location = New System.Drawing.Point(237, 120)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(140, 17)
        Me.Label9.TabIndex = 26
        Me.Label9.Text = "正在执行更新过程..."
        '
        'PictureBox7
        '
        Me.PictureBox7.Location = New System.Drawing.Point(197, 152)
        Me.PictureBox7.Name = "PictureBox7"
        Me.PictureBox7.Size = New System.Drawing.Size(20, 20)
        Me.PictureBox7.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.PictureBox7.TabIndex = 29
        Me.PictureBox7.TabStop = False
        '
        'Label10
        '
        Me.Label10.Location = New System.Drawing.Point(237, 154)
        Me.Label10.Name = "Label10"
        Me.Label10.Size = New System.Drawing.Size(140, 17)
        Me.Label10.TabIndex = 28
        Me.Label10.Text = "更新完成,删除临时文件"
        '
        'PictureBox9
        '
        Me.PictureBox9.BackColor = System.Drawing.SystemColors.HighlightText
        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(394, 56)
        Me.PictureBox9.TabIndex = 30
        Me.PictureBox9.TabStop = False
        Me.PictureBox9.Visible = False
        '
        'PictureBox8
        '
        Me.PictureBox8.Location = New System.Drawing.Point(197, 186)
        Me.PictureBox8.Name = "PictureBox8"
        Me.PictureBox8.Size = New System.Drawing.Size(20, 20)
        Me.PictureBox8.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.PictureBox8.TabIndex = 32
        Me.PictureBox8.TabStop = False
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(237, 189)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(140, 17)
        Me.Label7.TabIndex = 31
        Me.Label7.Text = "更新成功,开启应用程序"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(60, 154)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(123, 17)
        Me.Label3.TabIndex = 5
        Me.Label3.Text = "正在收集本机信息..."
        '
        'AxWebBrowser1
        '
        Me.AxWebBrowser1.ContainingControl = Me
        Me.AxWebBrowser1.Enabled = True
        Me.AxWebBrowser1.Location = New System.Drawing.Point(9, 11)
        Me.AxWebBrowser1.OcxState = CType(resources.GetObject("AxWebBrowser1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxWebBrowser1.Size = New System.Drawing.Size(376, 60)
        Me.AxWebBrowser1.TabIndex = 33
        '
        'GroupBox2
        '
        Me.GroupBox2.BackColor = System.Drawing.Color.AliceBlue
        Me.GroupBox2.Controls.Add(Me.AxWebBrowser1)
        Me.GroupBox2.Location = New System.Drawing.Point(0, -4)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(394, 75)
        Me.GroupBox2.TabIndex = 34
        Me.GroupBox2.TabStop = False
        '
        'update
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15)
        Me.ClientSize = New System.Drawing.Size(394, 399)
        Me.Controls.Add(Me.GroupBox2)
        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.Name = "update"
        Me.ShowInTaskbar = False
        Me.Text = "在线升级程序"
        Me.GroupBox1.ResumeLayout(False)
        CType(Me.ProgressCdownload.Properties, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.Progressdownload.Properties, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.GroupBox2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams '禁用系統關閉按鈕,屏蔽ALT+F4
        Get
            Const CS_NOCLOSE As Integer = &H200
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE
            Return cp
        End Get
    End Property

    '载入
    Private Sub update_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Dim a As String = CStr(Me.Handle.ToInt32)
        ' Dim b As Short = 0
        ' Dim flag As Object
        Dim SwfPath As String = Application.StartupPath & "\images\login.swf"
        ' Call Myapi.Disabled(CShort(a), CShort(b)) '让关闭X不可用
        Call Reset()
        Call GetAddress()
        ' MsgBox(SwfPath)

        AxWebBrowser1.Navigate(SwfPath)
   
        For x As Integer = 0 To 10
            Application.DoEvents()
        Next
        btnupdate_Click(New System.Object, New System.EventArgs) '執行更新

    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)
      
    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")
            SleepTime = Func.GetKeyVal(Inipath, "serverpath", "Time")
            RunProgrom = Func.GetKeyVal(Inipath, "serverpath", "Run")
            KillProgrom = Func.GetKeyVal(Inipath, "serverpath", "Kill")
        
        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 = "连接服务器失败..."
            PictureBox1.Image = Image.FromFile(Err)
            Resetbtn()
            Exit Sub
        End Try
    End Sub

    '检查更新版本 读取数据集 如果数据集为空则失败
    Private Sub Getnewversion()
        PictureBox2.Image = Image.FromFile(down)
        Try
            UpdateDataSet.ReadXml(Tempfolder & "\update.xml")
            '   MsgBox(updateDataSet.Tables(0).Rows.Count)
            If UpdateDataSet.Tables(0).Rows.Count <= 0 Or (UpdateDataSet Is Nothing) Then
                lbl.Text = "检查可更新版本失败..."
                Reset()
            End If
        Catch ex As Exception
            lbl.Text = "检查可更新版本失败..."
            PictureBox2.Image = Image.FromFile(Err)
            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()
            System.Threading.Thread.Sleep(2000) '1.5秒后打開主程序
            RunMainProgrom()
            Exit Sub
        Else
            Call ChkAlldownloadbtye() '獲取字節數
        End If
    End Sub
    Private Sub RunMainProgrom()
        On Error Resume Next
        Call DelTmpFiles()
        System.Diagnostics.Process.Start(Application.StartupPath & "\" & RunProgrom)
        ' Me.Close()
        ' Runthread.Abort()
        Application.Exit()
    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 = "升级失败,无法分析更新版本..."
            PictureBox3.Image = Image.FromFile(Err)
            Resetbtn()
            Exit Sub
        End Try
    End Sub
    Private Sub ChkAlldownloadbtye()
        Try
            For i As Integer = 0 To UpdateDataSet.Tables("file").Rows.Count
                Alldownloadbyte += UpdateDataSet.Tables("file").Rows(i)("filelength")
            Next
        Catch ex As Exception

        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 = Nothing
        Dim mbyte() As Byte
        Dim allbyte As Long
        Dim startbyte As Integer
        Dim m As Integer
        Dim fs As FileStream
        Dim myre As HttpWebRequest = Nothing
        Dim mwrite As HttpWebResponse
        Dim wc As WebClient = New WebClient
        'Dim myCredential As New NetworkCredential '("pengli@triopy", "fairy")
        'wc.Credentials = myCredential
        PictureBox4.Image = Image.FromFile(down)
        Progressdownload.Position = 0 '总的进度
        Progressdownload.Properties.Maximum = Alldownloadbyte
        ' MsgBox(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"))) '本也可以直接获取文件大小,但是很占用资源,干脆写在配置文件内
                ' MsgBox(UpdateDataSet.Tables("file").Rows(i)("filelength"))
                myre = CType(WebRequest.Create(UpdateDataSet.Tables("file").Rows(i)("downurl")), HttpWebRequest)
                ' MsgBox(UpdateDataSet.Tables("file").Rows(i)("downurl").ToString)
                mwrite = CType(myre.GetResponse(), HttpWebResponse)
                srm = wc.OpenRead(UpdateDataSet.Tables("file").Rows(i)("downurl"))
                allbyte = mbyte.Length
                ' MsgBox(allbyte)
                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.Create)
                'Try
                fs.Write(mbyte, 0, mbyte.Length)
                'Catch ex As Exception
                '    MsgBox(ex.ToString)
                '    Resetbtn()
                'End Try

                fs.Flush()
                fs.Close()
                myre.Abort() '这里必须释放资源,否则下载多个文件出现连接超时错误
                srm.Close()
                Thread.Sleep(SleepTime) '这里每下一个文件让线程等待2秒,太快可能服务器没有响应
            Next
            Progressdownload.Position = Alldownloadbyte '防止人为写错字节数不到100的现象
        Catch ex As Exception
            lbl.Text = "下载更新文件失败..."
            PictureBox4.Image = Image.FromFile(Err)
            Resetbtn()
            myre.Abort()
            srm.Close()

            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(KillProgrom) '这里是我引用的一个类,用来杀进程的
    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
                IO.File.Copy(Tempfolder & "\" & UpdateDataSet.Tables("file").Rows(i)("filename"), Application.StartupPath & UpdateDataSet.Tables("file").Rows(i)("target"), True)
            Next
        Catch ex As Exception
            lbl.Text = "升级到新版本失败,可能应用程序未关闭..."
            PictureBox6.Image = Image.FromFile(Err)
            Resetbtn()
            Exit Sub
        End Try
    End Sub

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

    '启动应用程序
    Private Sub Startexe()
        PictureBox8.Image = Image.FromFile(down)
        Try
            'Call DelTmpFiles()
            System.Diagnostics.Process.Start(Application.StartupPath & "\" & RunProgrom)
            ' Me.Close()
            Application.Exit()
        Catch ex As Exception
            lbl.Text = "更新成功,但未能启动应用程序,请手动启动..."
            PictureBox8.Image = Image.FromFile(Err)
        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
        ' updateDataSet.ReadXml(Application.StartupPath & "\update.xml")
        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
        On Error Resume Next
        Me.Hide()
        Call DelTmpFiles()
        System.Diagnostics.Process.Start(Application.StartupPath & "\" & RunProgrom)
        ' Me.Close()
        ' Runthread.Abort()
        Application.Exit()
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
        System.Diagnostics.Process.Start("IExplore.exe", "http://www.triopy.com/tmics/")
    End Sub
End Class

'類文件

Public Class func

    Private Myapi As New Fairy4_Api.My_Api

    Public Function cdbnull(ByVal str As String) As String
        Return str.Trim
    End Function
    Public Sub killprogress(ByVal strName As String)
 

默认分类 | 阅读 7517 次
文章评论,共0条
游客请输入验证码
浏览55855次
文章分类