Self updating application


I would like to add a self updating feature to my application. E.g. there is a central version number held on the server. When a user opens abc.exe with an old version, abc.exe should update itself with the latest version available on the server.

I know copying files very well, so this is not a problem. More the challenge is to overwrite a file that's in use. Does anyone know how to do this?

Also, I know this is possible with a seperate batch file or updating program, which I would like to avoid.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

updating the exe that is running could be bad - however if you stored your code in dll files you could drop the reference update the dll and relink it.
We considered trying thsi but weren't allowed for a variety of reasons.

Basic method
User executes a small app that checks version
If version ok then
  Execute the program
  Update the program (copy new exe, do an update install)
  Execute program

why not just end the current program from the program itself once as new version is detected

program itself checks the central server on every start for a new version
If version current then
  Execute the program
  open a webpage which will automatically download the new program from server
  end program
  if saving to the same directory, the browser will ask the user if he wants to overwrite

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

I have done this by placing a simple text file on the server containing the new or current version along with the install - and upon start up (sub Main) I check this to see if I should update the program.  If so, I kick off the update and exit the program:
Sub Main()
Dim sVerChk as String

'I load frmSplash & check user ID & stuff 1st ... THEN

'Ver Update code
On Error GoTo SkipChk
If Dir$("\\SERVER\APP\ver.txt") <> "" Then
  Open "\\SERVER\APP\ver.txt" For Input As #1
  Input #1, sVerChk
  Close #1
  If sVerChk = "LOCKED" Then
    MsgBox "This application has been locked by the developer.  Maintenance is under way and the " & _
      "application is incompatible until it is completed.  Once completed, this lock will be rescinded" & _
      " and you will be allowed entry." & vbCrLf & vbCrLf & "Please try again later.  Thanks for your " & _
      "patience.", vbOKOnly + vbSystemModal, "Access Denied"
    Unload frmSplash  
    Exit Sub
  End If
  If sVerChk > App.Major & "." & App.Minor & "." & App.Revision Then
    If MsgBox("A new version of your program is available!" & vbCrLf & _
      "You must upgrade or exit.  If you are dialed in remotely via modem this action is not advised." & vbCrLf & _
      "Once you begin this process you must complete it or undesirable results may occur." & vbCrLf & _
      vbCrLf & "Do you wish to run setup now?", vbOKCancel + vbQuestion + vbSystemModal, "UPDATE AVAILABLE") _
      = vbCancel Then
      Unload frmSplash
      MsgBox "You will need to perform the update before you can run the application.", _
        vbOKOnly + vbInformation, "UPDATE AVAILABLE"
      Exit Sub
    End If
    MsgBox "Please take all defaults while executing the setup program.  Changing any defaults can cause " & _
      " unpredictable results on future upgrades." & vbCrLf & vbCrLf & "Thank You for your cooperation.", _
      vbOKOnly + vbInformation, "Setup Instructions"
    rtn = Shell("\\SERVER\APP\setup.exe", vbNormalFocus)
    Unload frmSplash
    Exit Sub  'Ends the current invocation of the program
  End If
End If
GoTo OKCont
'Note that this code will allow the user to continue if the click OK instead of Cancel...
If MsgBox("Error: " & Err & " Desc: " & Err.Description & vbCrLf & "This occurred while attempting auto update." & _
  vbCrLf & vbCrLf & "( * If it is error 52, then you are not connected to the network or the path " & vbCrLf & _
  "\\SERVER\APP" & vbCrLf & "is not visible to your computer. * )", vbCritical + vbOKCancel + vbDefaultButton2, _
  "PROGRAM UPDATE ERROR") = vbCancel Then
  Unload frmSplash
  Exit Sub
End If

' And we execute our program as usual from here on out...
This approach allows me to lock people out while I make database changes or do other maintenance as well.  The ver.txt file simply contains a string like "3.4.1".  I used the > to check this but be careful, you may want to use <> instead based upon how your revision numbers go. For example: "3.4.11"  is NOT > "3.4.9"...
JezzaKashelAuthor Commented:
Hi trkcorp

Thanks for your comments, this looks to be what I'm after.

From what I've seen of your approach, you are updating the current exe (e.g. abc.exe) by running the setup on the server via the command rtn = Shell("\\SERVER\APP\setup.exe", vbNormalFocus).

Does this not cause some problems because your setup program will overwrite the abc.exe that is currently running. Acutally due to user rights etc I was just going to have a batch file that would copy the exe from the server. Will this work also?


    No problems; I kick off the install FROM the server, not ON it, and then immediately exit the application.
(Exit Sub  'Ends the current invocation of the program)  *IMPORTANT* When your start up object is Sub Main exiting that sub effectively ends the program...
    The install initialization has a splash and has to gather its "list" & all - it stops and asks questions just like any install normally does and it won't even attempt to overwrite the exe until you get to the real install part... The initiating exe is exited before install has a chance to even think about being in conflict.
    You are more likely to run into a conflict trying to copy the exe from within itself, but if you place a slight pause in the .bat file to allow a complete exit from the calling program before the copy begins you can probably avoid that conflict.  
    Personally, I would prefer the install (setup.exe) approach, although it is more cumbersome for the user, it is more professional looking and if you are making ongoing enhancements and bug fixes you will eventually most likely need to run a full install at some point anyhow.
JezzaKashelAuthor Commented:
Hi trkcorp

Thanks for that. I'll have to take the exe approach, as the users Pcs are seriously locked down with regards to permissions. They have no chance of registering Dlls etc.

Just one question, how do I put the pause in the batch file without the user having to click to continue. Is there not the equivalent of the vb wait command?


You can emulate a sleep-style command in a batch file by using PING.  Since PINGING takes a bit of time, use:

      PING localhost -n 10 > NULL

to ping your own machine 10 times.  >NULL suppresses screen output.

Not the nicest workaround, though...


jimbobmcgee - nice trick =]

i know the microsoft resource kit for the 2003 server has a sleep command.

The above suggestions are good or you could write a little VB interface to do the copy instead of using bat files.  The VB interface could take a second or two allowing ample time for the app to exit, and then even allow the user to cancel or go ahead if you so desired.  You could provide status on the copy operation and issue a message when complete.  You could even sort of reverse engineer using the logic above to allow the user to start their new version of the program (then end the copy pgm) if you want.  This little VB interface could also perform other tasks you may dream up as you go along...
Good luck!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JezzaKashelAuthor Commented:
Thanks for all the comments.

In fact, the sleep command was not necessary in the batch file, the exe gets updated well with just the line:

copy "\\Server\abc.exe" "C:\Program Files\abc"

So all is good

Many thanks

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.