This code helps you to
Quote:
Check the internet connection
Check for a new version
Download the new version
Launch the new version
How it works?
Quote:
The idea is to compare Application.MajorVersion, Application.MinorVersion and Application.BugVersion with a text file on the server. The text file becomes for every new major release a new line. For example you release your application first time. Application.MajorVersion, Application.MinorVersion and Application.BugVersion have this values: 1.0.0, your text file have this values too. If there is a new update, you change only the second and third value in the text file and the values for Application.MinorVersion and Application.BugVersion and upload the new version to the server. If there is a new major release, you add a new line to the text file e.g. 2.0.0.
You just need to adjust DownloadPath, DownloadURL and VersionURL, the rest will work automatic. To test the code below without a internet connection, remove the code in IsUpdateAvailable and add only "return true". For test we download REAL Studio. Once the download is finished, the application will be closed and the installer launched.
1. Create a new text file and reanme it to version.txt and add 1.1.0 as the first line
2. Upload the text file version.txt to your server
3. Remove all Windows and add 2 new Windows: MainWindow and UpdateWindow
4. Uncheck the Visible property in the IDE for MainWindow and UpdateWindow
5. Add a TCPSocket to UpdateWindow and rename it to UpdateSocket and change the super to HTTPSocket
6. Add a ProgressBar to UpdateWindow and make the maximum value the same as the width value
7. Add a module and rename it to UpdateModule
8. Add this properties to UpdateModule
DownloadPath As String = REALStudioSetup.exe
DownloadURL As String = http://realsoftware.cachefly.net/REALStudio2010r32/REALStudioSetup.exe
VersionURL As String = http://www.yourdomain.com/version.txt
9. Add this function to UpdateModule
Function IsUpdateAvailable() As Boolean
// arr(0) is the first line, arr(1) the seconde line, etc.
// for every new major release add a new line to the text "version.txt"
// for every new minor or bug version change the line which are equal to the app.majorversion
dim major, minor, bug as integer
dim s, updateversion, arr(-1) as string
dim h as new httpsocket
updateversion = h.get(VersionURL, 5) // get the lines
arr = ReplaceLineEndings(updateversion, endofline).split(endofline) // add each line to the array arr()
if ubound(arr) < 0 then return false
s = arr(0) // assume this app.majorversion is 1, so we need to check the first line
major = val(NthField(s, ".", 1)) // add the first number before the first point
minor = val(NthField(s, ".", 2)) // add the second number after the first point
bug = val(NthField(s, ".", 3)) // add the third number after the second point
if major > app.majorversion or minor > app.minorversion or bug > app.bugversion then // check if any value is greater
return true
else
return false
end if
End Function
10. Add this to App.Open
dim f as folderitem = temporaryfolder.child(DownloadPath)
dim d as new messagedialog
dim b as messagedialogbutton
d.icon = messagedialog.graphicnote
d.alternateactionbutton.visible = true
d.title = "Update"
d.actionbutton.caption = "Yes"
d.alternateactionbutton.caption = "No"
d.message = "A new version is available."
d.Explanation = "Download?"
if IsUpdateAvailable = true then
b = d.showmodal
select case b
case d.actionbutton
updatewindow.updatesocket.get(DownloadURL, f)
else
mainwindow.show
end select
else
mainwindow.show
end if
11. Add this to UpdateWindow.UpdateSocket.DownloadComplete
dim f as folderitem = temporaryfolder.child(DownloadPath)
if f <> nil and f.exists = true then
f.launch
quit
end if
12. Add this to UpdateWindow.UpdateSocket.ReceiveProgress:
progressbar1.value = round( (bytesreceived/totalbytes) * progressbar1.maximum )