Not a Member Yet,
Click here to Register

ID: 372
Viewed: 6470
Added: Aug 19, 2002
Version:
Snippet uploaded by: snippet
Written By: Unknown
Demo: Sorry, no demo



User Rated at: 0 Stars
Rate This:

Thank you for your vote. Please wait...

It appears you already voted for this snippet

It appears your vote value was empty

Creating an auto update function
Dateline: 03/10/99

Introduction

For those readers who are running Windows 98, you
may have noticed that whenever you try to install a new piece of hardware, the
operating system dials into the Microsoft server to try and download the very
latest version. In this article, I will show you how
to use the Microsoft Internet controls to build in an Auto Update feature into
your own applications.

The Principle
The idea is that there will be a
command button or menu item within your program that the user can use to check
for the latest version of the program on a web server. This command button will
run an external "updater" program, that will dial-in to the web server
and download a current version file, that contains the version number of the
very latest program. If the program finds a more
recent version than the one installed, it will attempt to download the updated
files. For the purposes of demonstration, I have
create a very simple program that will display its version information in a
message box whenever it is run. Another program, which we will build in this
article, will act as an updater, checking the web server for an updated version
of the file whenever it is run.

Technical Ideas
In theory, you should be able to keep a text file on the
Internet that contains information about the current version of your program.
On the local computer, you will also keep a similar
file with information about the current version installed. The Update program
will compare the two files, and if a more recent version of the program exists
online, it will download the program and overwrite the older version.


Downloading files using the Internet Transfer Control
Along with VB5 came
an extra control called the Internet Transfer Control. This handy ActiveX can be
used to connect to a certain web server and download files. In this case, we will use it to download the current
version text file.

Structure of the version text file
Each version file will contain a number that corresponds to
the version of the program. For example, version 1.0
of a program might have the number "000001" or even "1000"
in the file. For the purposes of this article, this
is all that there is in this file, although you may wish to include lists of
files, each with its own version number.

Let's Go!

To start with, we will build our simple
updater program. It needs to do the following tasks in order to work properly:


Open the local version file and read in the
number

Download the server version file and save it in a temporary file
on the local computer.

Open the server version
file and read in the number

Compare the
version numbers from the server and the local computer.

If the server number is larger than the local computer
number, announce to the user that a new version of the program exists. Ask the
user if they want to download an update.

If
the user clicks no, exit the program.

Else (!)
download the latest version of the program from the server and overwrite the
local computer copy.

Update the local version file with the number from
the server version file.

Announce to the user that the update was a
success!

Code
Create a new project, with one form. Add a command button onto the form with the Caption
"Check for Updates" and the name, "cmdUpdate". Right-click
the toolbox and select "Components". From the list of controls, tick
the box next to "Microsoft Internet Transfer Control". An icon will
appear in the toolbox:

Click on this icon, and add the Internet Transfer
control to the form. Rename it "InetUpdate"

OK! Now, add the following code to the cmdUpdate_Click event:

Highlight all by clicking in box
<!---Declaration--->
none

Highlight All
<!---Code--->
Dim intLocalVer 
As Integer
Dim b() As Byte
Dim intRemoteVer As Integer
Dim strRemoteVer As String
Dim doUpdate As Boolean

'1. Open the local version file and read in the number


Open App.Path & "curversion.dat"
For Input As #1

intLocalVer = CInt(Input(LOF(1), 1))
Close 1


'2. Download the remote version file and read in the
number
' Note: This is all one line:


b() = InetUpdate.OpenURL("http://visualbasic.about.com/library/weekly/remotever.dat", 1)

strRemoteVer = ""


For t = 0 To UBound(b)
strRemoteVer = strRemoteVer + Chr(b(t))
Next


intRemoteVer = Int(strRemoteVer)

'3. Compare numbers

If
intRemoteVer > intLocalVer Then
'Note: This is all one line:
If MsgBox("A more recent version of this program exists. Would you like to update it now?", vbYesNo Or vbQuestion) = vbYes Then

doUpdate = True
Else
doUpdate = False

End If
Else
MsgBox "You already have the most recent version of this program."
doUpdate = False
End If

'4. If doupdate = True, then download the latest program exe from
the site

If doUpdate Then
'Note: This is all one line:

b() = InetUpdate.OpenURL("http://visualbasic.about.com/library/weekly/update.exe", 1)

Open
App.Path & "update.exe" For Binary
Access Write As #1
Put #1, , b()
Close 1

Kill App.Path & "program.exe"
Name App.Path & "update.exe" As App.Path & "program.exe"

'Now save the current version into the local version file


Open App.Path &
"curversion.dat" For Output As #1
Print #1, strRemoteVer

Close 1

MsgBox "Update
Complete!"

End If;


No Comments to show

Please completely fill out the form below if you want to review this snippet. All reviews are subject to validation.


Replying to a Comment...


Adding your comment. Please wait...

Thanks for adding your comment!. After further review it will be added.

There was a problem adding your comment. Please try again.

Please complete all the fields in the form before sending.

© 2002 - 2024 snippetlibrary.com All Rights Reserved. Conditions
Do NOT follow this link or you will be banned from the site!