[VB.NET] Auto Updater

Results 1 to 1 of 1
  1. #1
    Digital Horizon KristiansJ is offline
    MemberRank
    Jul 2012 Join Date
    203Posts

    [VB.NET] Auto Updater

    I saw some guides how to make auto updaters in this selection, but their concept was bad becouse user needed to install winzip to update client.

    Tools
    Visual Basic 2008: Download: Visual Studio 2008 Express Edition - Microsoft Download Center - Download Details
    DotNetZip Library: DotNetZip Library

    Features
    • Good Error handling
    • Password protected archives
    • Simple to use


    Full Source
    PHP Code:
    Imports Ionic.Zip

    Module AutoUpdate

    #Region "Variables"
        
    Dim _updateServerURL As String
        Dim _currentVersion 
    As Integer
        Dim _latestVersion 
    As Integer
        Dim _tempCurrentVersion 
    As String
        Dim _tempLatestVersion 
    As String
        Dim _updateArchivePassword 
    As String
        Dim _LastError 
    As String
    #End Region

    #Region "Property"
        
    Property CurrentVersion() As Integer
            Get
                
    Return _currentVersion
            End Get
            Set
    (ByVal value As Integer)
                
    _currentVersion value
            End Set
        End Property

        Property LatestVersion
    () As Integer
            Get
                
    Return _latestVersion
            End Get
            Set
    (ByVal value As Integer)
                
    _latestVersion value
            End Set
        End Property

        Property ArchivePassword
    ()
            
    Get
                
    Return _updateArchivePassword
            End Get
            Set
    (ByVal value)
                
    _updateArchivePassword value
            End Set
        End Property

        Property UpdateServer
    () As String
            Get
                
    Return _updateServerURL
            End Get
            Set
    (ByVal value As String)
                
    _updateServerURL value
            End Set
        End Property
    #End Region

        
    Sub Start()
            Try
                
    My.Computer.Network.DownloadFile(String.Format("{0}/updates/latest_ver.txt"UpdateServer), Application.StartupPath "/update/latest_ver.txt"NothingNothingFalse1000True)
            Catch 
    ex As Exception
                _LastError 
    "Update Server is Offline or Version file not found!"
                
    GoTo UPDATE_FAILED
            End 
    Try

            Try
                
    _tempCurrentVersion My.Computer.FileSystem.ReadAllText("update/current_ver.txt").Trim
            
    Catch ex As Exception
                _LastError 
    "Current Version information file, not found!"
                
    GoTo UPDATE_FAILED
            End 
    Try

            Try
                
    _tempLatestVersion My.Computer.FileSystem.ReadAllText("update/latest_ver.txt").Trim
            
    Catch ex As Exception
                _LastError 
    "Latest Version information file, not found!"
                
    GoTo UPDATE_FAILED
            End 
    Try

            Try
                
    CurrentVersion Convert.ToInt32(_tempCurrentVersion)
                
    LatestVersion Convert.ToInt32(_tempLatestVersion)
            Catch 
    ex As Exception
                _LastError 
    "Current/Latest Version string convertion to int32 failed!"
                
    GoTo UPDATE_FAILED
            End 
    Try

            If 
    CurrentVersion LatestVersion Then
                _LastError 
    "You are already running the latest version!"
                
    GoTo UPDATE_FAILED
            
    ElseIf CurrentVersion LatestVersion Then
                _LastError 
    "Current Version is bigger then latest version!"
                
    GoTo UPDATE_FAILED
            End 
    If

            GoTo 
    UPDATE_START

    UPDATE_START
    :
            Do 
    Until CurrentVersion LatestVersion

                
    Try
                    
    My.Computer.Network.DownloadFile(String.Format("{0}/updates/update_{1}.zip"UpdateServerCurrentVersion), String.Format(Application.StartupPath "/update/titan_update_{0}.upd"CurrentVersion), NothingNothingTrue1000True)
                Catch 
    ex As Exception
                    _LastError 
    String.Format("Update Package '{0}' download have failed! "CurrentVersion)
                    GoTo 
    UPDATE_FAILED
                End 
    Try

                Try
                    
    Using zip As ZipFile ZipFile.Read(String.Format("update/titan_update_{0}.upd"CurrentVersion))
                        
    zip.Password ArchivePassword
                        
    For Each zip_entry As ZipEntry In zip
                            zip_entry
    .Extract(Application.StartupPathExtractExistingFileAction.OverwriteSilently)
                        
    Next zip_entry
                    End Using
                
    Catch ex As Exception
                    _LastError 
    String.Format("Update Package '{0}' extraction have failed! "CurrentVersion)
                    GoTo 
    UPDATE_FAILED
                End 
    Try

                
    CurrentVersion += 1
            Loop

            
    If CurrentVersion LatestVersion Then
                
    GoTo UPDATE_FINISH
            
    Else
                
    _LastError String.Format("Client update have failed!")
                GoTo 
    UPDATE_FAILED
            End 
    If

    UPDATE_FINISH:
            Try
                
    My.Computer.FileSystem.DeleteFile("update/current_ver.txt")
                
    My.Computer.FileSystem.RenameFile("update/latest_ver.txt""current_ver.txt")
            Catch 
    ex As Exception
                _LastError 
    String.Format("Update information save failed!")
                GoTo 
    UPDATE_FAILED
            End 
    Try

            
    _LastError "Your client have been updated!"
            
    GoTo UPDATE_FAILED

    UPDATE_FAILED
    :
            
    MessageBox.Show(_LastError"Auto Update")
            
    Application.Exit()
        
    End Sub

    End Module 
    Usage
    PHP Code:
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgsHandles MyBase.Load
            AutoUpdate
    .UpdateServer "http://titanstudios.net/"
            
    AutoUpdate.ArchivePassword "titanUpdateFile"
            
    AutoUpdate.Start()
        
    End Sub 
    Client Folder
    ./client/update/current_ver.txt
    Code:
    1
    WebServer
    http://xxx.xx/updates/last_ver.txt
    Code:
    2
    Update Archives
    To make update archive add needed file to zip archive if needed add password, then rename
    archive to update_ID.zip (Ex: update_2.zip) and then put it in http://xxx.xx/updates/ web folder

    Notes
    If client have update version 1 and latest update version for example is 5 then updater will download all updates from 1 to 5, comulative updates are not supported in this release!




Advertisement