Jump to content

  •  

  • iBotModz CBox


    yoda302055

    Member Since 05 Aug 2009
    Offline Last Active Jun 22 2013 09:11 PM
    -----

    #112735 How Do You Auto update in vb 08?

    Posted by yoda302055 on 31 January 2010 - 09:09 PM

    How Do You Auto update in vb 08?
    is it This?




    Imports System.IO
    Imports System.Net
    Module Main
        Public Sub Main()
            Dim ExeFile As String ' the program that called the auto update
            Dim RemoteUri As String ' the web location of the files
            Dim Files() As String ' the list of files to be updated
            Dim Key As String ' the key used by the program when called back 
            ' to know that the program was launched by the 
            ' Auto Update program
            Dim CommandLine As String ' the command line passed to the original 
            ' program if is the case
            Dim myWebClient As New WebClient ' the web client
            Try
                ' Get the parameters sent by the application
                Dim param() As String = Split(Microsoft.VisualBasic.Command(), "|")
                ExeFile = param(0)
                RemoteUri = param(1)
                ' the files to be updated should be separeted by "?"
                Files = Split(param(2), "?")
                Key = param(3)
                CommandLine = param(4)
            Catch ex As Exception
                ' if the parameters wasn't right just terminate the program
                ' this will happen if the program wasn't called by the system 
                ' to be updated
                Exit Sub
            End Try
            Try
                ' Process each file 
                For i As Integer = 0 To Files.Length - 1
                    Try
                        ' try to rename the current file before download the new one
                        ' this is a good procedure since the file can be in use
                        File.Move(Application.StartupPath & "\" & Files(i), _
                            Application.StartupPath & "\" & _
                            Now.TimeOfDay.TotalMilliseconds & ".old")
                    Catch ex As Exception
                    End Try
                    ' download the new version
                    myWebClient.DownloadFile(RemoteUri & Files(i), _
                        Application.StartupPath & "\" & Files(i))
                Next
                ' Call back the system with the original command line 
                ' with the key at the end
                System.Diagnostics.Process.Start(ExeFile, CommandLine & Key)
                ' do some clean up -  delete all .old files (if possible) 
                ' in the current directory
                ' if some file stays it will be cleaned next time
                Dim S As String = Dir(Application.StartupPath & "\*.old")
                Do While S <> ""
                    Try
                        File.Delete(Application.StartupPath & "\" & S)
                    Catch ex As Exception
                    End Try
                    S = Dir()
                Loop
            Catch ex As Exception
                ' something went wrong... 
                MsgBox("There was a problem runing the Auto Update." & vbCr & _
                    "Please Contact Admin at MMG.com" & vbCr & ex.Message, _
                    MsgBoxStyle.Critical)
            End Try
        End Sub
    End Module
    Public Class Form4
        Public Class AutoUpdate
            Public Function AutoUpdate(ByRef CommandLine As String) As Boolean
                Dim Key As String = "&**#@!" ' any unique sequence of characters
                ' the file with the update information
                Dim sfile As String = "update.dat"
                ' the Assembly name 
                Dim AssemblyName As String = _
                        System.Reflection.Assembly.GetEntryAssembly.GetName.Name
                ' here you need to change the web address 
                Dim RemotePath As String = _
                    "http://MasterModdingGroupMMGD.weebly.com/"
                ' where are the files for a specific system
                Dim RemoteUri As String = RemotePath & AssemblyName & "/"
                ' clean up the command line getting rid of the key
                CommandLine = Replace(Microsoft.VisualBasic.Command(), Key, "")
                ' Verify if was called by the autoupdate
                If InStr(Microsoft.VisualBasic.Command(), Key) > 0 Then
                    Try
                        ' try to delete the AutoUpdate program, 
                        ' since it is not needed anymore
                        System.IO.File.Delete(Application.StartupPath & "\autoupdate.exe")
                    Catch ex As Exception
                    End Try
                    ' return false means that no update is needed
                    Return False
                Else
                    ' was called by the user
                    Dim ret As Boolean = False ' Default - no update needed
                    Try
                        Dim myWebClient As New System.Net.WebClient 'the webclient
                        ' Download the update info file to the memory, 
                        ' read and close the stream
                        Dim file As New System.IO.StreamReader( _
                            myWebClient.OpenRead(RemoteUri & sfile))
                        Dim Contents As String = file.ReadToEnd()
                        file.Close()
                        ' if something was read
                        If Contents <> "" Then
                            ' Break the contents 
                            Dim x() As String = Split(Contents, "|")
                            ' the first parameter is the version. if it's 
                            ' greater then the current version starts the 
                            ' update process
                            If x(0) > Application.ProductVersion Then
                                ' assembly the parameter to be passed to the auto 
                                ' update program
                                ' x(1) is the files that need to be 
                                ' updated separated by "?"
                                Dim arg As String = Application.ExecutablePath & "|" & _
                                            RemoteUri & "|" & x(1) & "|" & Key & "|" & _
                                            Microsoft.VisualBasic.Command()
                                ' Download the auto update program to the application 
                                ' path, so you always have the last version runing
                                myWebClient.DownloadFile(RemotePath & "autoupdate.exe", _
                                    Application.StartupPath & "\autoupdate.exe")
                                ' Call the auto update program with all the parameters
                                System.Diagnostics.Process.Start( _
                                    Application.StartupPath & "\autoupdate.exe", arg)
                                ' return true - auto update in progress
                                ret = True
                            End If
                        End If
                    Catch ex As Exception
                        ' if there is an error return true, 
                        ' what means that the application
                        ' should be closed
                        ret = True
                        ' something went wrong...
                        MsgBox("There was a problem runing the Auto Update." & vbCr & _
                            "Please Contact Admin:mastermoddinggroup.weebly.com" & vbCr & ex.Message, _
                            MsgBoxStyle.Critical)
                    End Try
                    Return ret
                End If
            End Function
        End Class