Click here to Skip to main content
14,599,199 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi. So I have made a auto updater for my program. So the code is given, using Dropbox. So what my question is:
My program's version is v1.0 and when updating it then delete old version and keep the new version.
Another question.
How can I set it, when program is opened to recognize that is in v1.0 and once the version.txt is changed then recognize v1.1 for example?

What I have tried:

Public Sub CheckForUpdates()
        Dim file As String = Application.StartupPath & "/version.txt"
        Dim MyVer As String = My.Application.Info.Version.ToString
        If My.Computer.FileSystem.FileExists(file) Then
            My.Computer.FileSystem.DeleteFile(file)
        End If
Try
            My.Computer.Network.DownloadFile("https://www.dropbox.com/s/blalblakasdd/version.txt?dl=1", file)
        Catch ex As Exception
            MsgBox(ex.Message + " You are up to date.")
        End Try
        Dim LastVer As String = My.Computer.FileSystem.ReadAllText(file)
        If MyVer < LastVer Then
            MsgBox("Update Available!")
Try
                My.Computer.Network.DownloadFile("https://www.dropbox.com/s/blalblakasd/program.exe?dl=1", Application.StartupPath + "/program.exe")
            Catch ex As Exception
                MsgBox(ex.Message + " Error Downloading update.")
            End Try
            Process.Start(Application.StartupPath + "/Latest.exe")
        Else
            MsgBox("Program is up to date")
        End If
    End Sub
Posted
Updated 16-Jan-19 6:48am
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can use the ApplicationDeployment.CurrentVersion property to read the application version that is set in the Project Properties
ApplicationDeployment.CurrentVersion Property (System.Deployment.Application) | Microsoft Docs[^]

Version has properties of its own for Major, Minor, Revision, and Build numbers
Version Class (System) | Microsoft Docs[^]

There is also some sample code on those pages which may help you out as well
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Did you know it's even possible to make your application self-destruct ?
This can be handy if you only want to use one executable, see CodeProject article here: How To Make Your Application Delete Itself Immediately[^]
There is even another method: rename the running exe so the new exe can be placed, strangely enough this is allowed by Windows.
But if you are satisfied with your current solution, then there is no need for such dirty tricks of course !
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

IF your code is doing what it looks like it's coded to do, you've got a huge design problem.

First, the app you launch should NOT be your main application. It should be a separate app that checks the installed version of the application against the server.

Next, when you download the new executable, you cannot write it to the same file that was launched. An executable file is locked while it is running so there is no way for an application to overwrite itself.

Another problem. If you install your app to a folder under Program Files, the folder is going to be ReadOnly to normal users. Users will never be able to update the application unless they have administrator rights on the machine.

What you should be doing is writing a separate application that checks the version of the application that is installed against the server version. If the installed version needs to be updated, download the new files and place them in a location that users can write to, like a folder under C:\ProgramData, or CommonApplicationData when using Environment.GetFolderPath().

If the installed version of the app matches whats available on the server, just launch the app from the folder it's installed to above.
   
Comments
Maciej Los 15-Jan-19 17:22pm
   
This is it! I wanted to say exactly the same!
5ed!
Rate this:
Please Sign up or sign in to vote.

Solution 4

As an alternative, ClickOnce[^] is another option and removes all the hard work for you. I have written a wrapper to help make using ClickOnce a lot easier... You can find it here: Silent ClickOnce Installer for Winform & WPF in C# & VB[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 5

Thanks to y'all! You really helped me a lot and succeed! :)
   
Comments
Richard Deeming 18-Jan-19 9:34am
   
If you want to reply to a solution, click the "Have a Question or Comment?" under that solution. DO NOT post your comment as a new "solution".

If one or more solutions helped you to solve your problem, then you should click the button to accept them. That way, your question will be marked as "solved".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100