|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis code enables an application to update itself when there is a new version available. The auto update is transparent to the user and checks for new updates every time the user launches the program. This code was written to be used in a controlled environment (intranet) and is not intended to be used through the internet due to security issues. BackgroundI've been developing client/server systems for a long time, and the worst part of the software life cycle is to update it in all users' machine. The worst scenario is when you need to make a database change, and all the users are required have the latest version, otherwise the system will crash. Today a lot of systems offer the ability to auto update, what makes the programmer's and the user's life easier. Looking through the internet, I couldn't find any code that I could use in my projects in a simple way, so I decided to write my own code. Some points that I'd like to have in an auto update program are:
How it worksIn the program that you want to be auto updateable, you just need to call the The codeThe auto update programThis is the code for the auto update program. The program is a windowless application with only one 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 [contact info]" & vbCr & ex.Message, _
MsgBoxStyle.Critical)
End Try
End Sub
End Module
The auto update class/functionThis is the class with the This function receives a parameter by reference, the command line passed to the program, and can be the original command line or the one passed by the Auto Update program. If it was sent by the Auto Update program, the Here you need to change the 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://[the web address for the update folder root]/"
' 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 [contact info]" & vbCr & ex.Message, _
MsgBoxStyle.Critical)
End Try
Return ret
End If
End Function
End Class
The auto update web folderThe auto update web folder should have a folder for each system you want to upgrade. The root folder is the one that you will refer on the 1.2.1234.5543|MyProgram.exe?file1.txt?file2.cfg
Using the codeWell, now to use the code is very simple. In the application where you want to enable the auto update, just call the ' The variable with the command line (to replace the
' Microsoft.VisualBasic.Command() function)
Public CommandLine As String
Public Sub Main()
' if you are using class create it
Dim MyAutoUpdate As New AutoUpdate
' test if an update is needed and quit the program if so.
If MyAutoUpdate.AutoUpdate(CommandLine) Then Exit Sub
' here goes your regular code in the main sub
End Sub
Points of interestThis auto update was written with the intention to always check for a new version, since the application involved can have problems if it runs in an old version. It guarantees that everyone who is using the application is running the latest version. This can be easily changed to update if something is available but don’t crash if it’s not. This program works fine for small sized programs and a few files. If you need to download large files I suggest you to open a splash screen and give a message to the user that something is happening. Limitations
|
||||||||||||||||||||||