Real Software Forums
http://forums.realsoftware.com/

Update checker
http://forums.realsoftware.com/viewtopic.php?f=21&t=35893
Page 1 of 3

Author:  serd83 [ Fri Oct 15, 2010 9:21 pm ]
Post subject:  Update checker

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 )

Author:  waveuponwave [ Fri Oct 15, 2010 9:41 pm ]
Post subject:  Re: Update checker

This is cool Serd I'll check it out. Thanks.

Author:  serd83 [ Fri Oct 15, 2010 9:45 pm ]
Post subject:  Re: Update checker

I upload a sample project:
http://www.file-upload.net/download-2893658/Update.rbp.html

Author:  serd83 [ Sat Oct 16, 2010 7:43 am ]
Post subject:  Re: Update checker

I edit the code above and upload a new sample project:
http://www.file-upload.net/download-2894224/Update.rbp.html

Author:  charonn0 [ Sat Oct 16, 2010 4:57 pm ]
Post subject:  Re: Update checker

What happens if:

//version running is 2.0.0
App.Majorversion = 2
App.Minorversion = 0
App.Bugversion = 0
//Version on the server is 1.9.9
Major=1
Minor=9
Bug=9

Author:  serd83 [ Sat Oct 16, 2010 9:44 pm ]
Post subject:  Re: Update checker

Maybe you understand this not correct. You start the text file with the first line. This is 1.x.x. For every new major release you add a new line e.g. 2.x.x, 3.x.x etc. If the first major release is 1, you compare the first line: arr(0), for major release 2: arr(1) etc.
Assume this is major release 1:
//version running is 1.0.0
App.Majorversion = 1
App.Minorversion = 0
App.Bugversion = 0

You compare arr(0):
//Version on the server is 1.9.9
Major=1
Minor=9
Bug=9

Assume this is major release 2:
//version running is 2.0.0
App.Majorversion = 2
App.Minorversion = 0
App.Bugversion = 0

You compare arr(1):
//Version on the server is 2.9.9
Major=2
Minor=9
Bug=9

Author:  serd83 [ Mon Oct 18, 2010 12:59 pm ]
Post subject:  Re: Update checker

I add a new variable MajorRelease to the function IsUpdateAvailable. This way you must not write every time a number to arr() and the code is a bit clearer.
Function IsUpdateAvailable() As Boolean
// 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
dim MajorRelease as integer = app.majorversion-1 // see which line is equal to app.majorversion

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(MajorRelease)

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

http://www.file-upload.net/download-2900048/Update.rbp.html

Author:  jlcurty [ Mon Nov 15, 2010 9:12 am ]
Post subject:  Re: Update checker

your example works great ! I only have one stupid question... where do I set the MajorVersion value ?
when I search for it I find : App.MajorVersio(nom de la propriete) Name :1
but when I double click on it, I don't have access to the value ...
thanks
jlc

Author:  Goofie [ Mon Nov 15, 2010 9:21 am ]
Post subject:  Re: Update checker

dim f as folderitem = temporaryfolder.child(DownloadPath)

should be
dim f as folderitem = specialfolder.temporary.child(DownloadPath)

in 2010 r4 :D

For the rest perfect code, thanks for sharing!

Author:  serd83 [ Wed Nov 17, 2010 7:34 pm ]
Post subject:  Re: Update checker

Thanks for the correction Goofie.

jlcurty,

Once you select App in the project tab, you can change the values for MajorVersion, MinorVersion and BugVersion. The values are Read-Only, therefore you must set it this way:
Image

Author:  medbase [ Thu Dec 09, 2010 10:30 am ]
Post subject:  Re: Update checker

Is the sample project still available.

I can't seem to acces the link

http://www.file-upload.net/download-290 ... e.rbp.html

Nikolas

Author:  Markus Winter [ Thu Dec 09, 2010 1:36 pm ]
Post subject:  Re: Update checker

You can get it from here:

http://dl.dropbox.com/u/992591/REALbasic/Forum/Update.rbp.zip

Author:  prakashp [ Tue Jan 10, 2012 8:44 am ]
Post subject:  Re: Update checker

Hi Serd,

Thanks for sharing this tip.

I have few questions on this.

Que #1
DownloadPath As String = REALStudioSetup.exe
DownloadURL As String = http://realsoftware.cachefly.net/REALSt ... oSetup.exe
VersionURL As String = http://www.yourdomain.com/version.txt

My Ques : I have a desktop app. I want to update my application from my client server where the latest "update.exe" is resides and which is inside a FTP folder. Then how to download the new version of that application to my machine because i don't have any "DownloadURL" as well as "DownloadPath". I have only the RemoteHost, RemoteUser and RemotePassword ?

Que #2
Dim f a folderitem = temporaryfolder.child(DownloadPath)

My Qus : Is there any way to get the Application.StartupPath using RB ? (I am VB.NET developer. In VB this is possible to get that path)

Thanks,
Prakash

Author:  serd83 [ Wed Jan 25, 2012 3:52 pm ]
Post subject:  Re: Update checker

Hi prakashp,

DownloadPath is not important. You can name this as you want like "update.exe" or "something.exe". Just be sure that ".exe" is at the end.

DownloadURL is important because this is the file on the server which you want to download. Simple upload your "update.exe" to your server into any folder. For example if you have a folder "downloads" and upload your "update.exe" there, so DownloadURL should be:
DownloadURL As String = http://www.yourdomain.com/downloads/update.exe


For the start up change

temporaryfolder.child(DownloadPath)


to

StartupItemsFolder

Author:  prakashp [ Fri Jan 27, 2012 1:17 am ]
Post subject:  Re: Update checker

Thanks Serd.

I got your point. I have a small doubt about this.

You pointed out that so DownloadURL should be

DownloadURL As String = http://www.yourdomain.com/downloads/update.exe


Can i change to :

DownloadURL As String = http://ftp.XXX.com/update.exe



Thanks,
Prakash

Page 1 of 3 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/