# Vb.net Updater



## Lillebror (May 27, 2009)

Hey All!

Ive ben looking around on the net for such a long time, without finding some easy way to make and updater. So i made one myself from scratch! 
The code aint pretty, and is kinda messy, but this is the first time that im making an updater.

The form consist of 2 labels, with default names, and 1 button, with default name aswell.



```
Imports System.IO
Imports System.Xml


Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       'Check if your program got a smaller version number, than the one on the net
 If Version_Program() < Version_OnNet() Then


            Try
                'Delete the old file first, so we dont get a conflict when downloading the new version
                File.Delete("Updater.exe")

                'Download the new version from a static place
                My.Computer.Network.DownloadFile("http://flumse.dk/Updater.exe", Application.StartupPath & "/Updater.exe", "", "", False, 500, True)
               
                'The thread is halted until the download is completed - so we start the program again when the updater is done
                Process.Start("Updater.exe")
                'And here we close the updating application
                Application.Exit()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        'Here we just check if the application number is the same as on the internet - if it is, do nothing
        ElseIf Version_Program() = Version_nettet() Then

        End If


    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Just 2 labels on the form, that tells the newest version and your version
        Label1.Text = ("You have version: " & Version_Program())

        Label2.Text = ("Newest is: " & Version_nettet())


    End Sub

    Public Function Version_Program()

        'Here we dim the path to the file we want to update, so we can se its version number
        Dim myBuildInfo As FileVersionInfo = FileVersionInfo.GetVersionInfo(Application.StartupPath & "/updater.exe")

        'Here we just makes it so our function name turns into the version of our file
        Version_Program = myBuildInfo.ProductVersion.ToString

    End Function

    Public Function Version_nettet()

        'Here we initialize our xml reading functions
        Dim xmlRead As XmlTextReader


        Try
            'Path to xml file
            xmlRead = New XmlTextReader("http://www.flumse.dk/test/test.xml")
            'Here we remove all whitespaces - we dont need those
            xmlRead.WhitespaceHandling = WhitespaceHandling.None
            'Here we read the first line of the xml file - since we dont need that line, we go to the next one
            xmlRead.Read()
            'We dont need this line either - go to next one
            xmlRead.Read()
            'Now we end at the line we want to read
            xmlRead.Read()
            'Find the element we need to read
            Dim Number = xmlRead.ReadElementString("version")
 
            Version_nettet = Number

            
            'Close the Xml reading
            xmlRead.Close()

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Function
End Class
```

And here is the XML code


```
<?xml version="1.0" encoding="utf-8"?>
<program>
  <version>3.1.4.0</version>
</program>
```


----------



## FordGT90Concept (May 27, 2009)

You really need not use XML.  You could just do an text file, for example, with the version number in it:


```
3.1.4.0
```

Use WebClient.DownloadFile to get it then parse the string for major, minor, revision, and build information (I think there is a Parse method to do this but I'd have to dig for it).


I'd recommend using WebClient.DownloadFile for the executables too.


When executing a process, it is recommended to set the working directory to that of the executable being ran.  If you don't, it could cause unexpected behaviors/errors.\


It's a good start there.


----------



## Lillebror (May 27, 2009)

Yeah, i could use a text file, but my plan is to make it just an update application to my older programs and future programs. So that my one xml file can hold the version number of all my programs 

Oh, your right - i should set it to 'Application.StartupPath/program' instead of just 'program' ! Thanks!


----------



## FordGT90Concept (May 27, 2009)

In that case, I would make an INI'esque text file with the following layout:

```
my app 1=3.1.5.0
my app 2=1.2.3.0
my app 3=9.3.5.0
```

To processs that, all you got to do is ReadLine, Split it on the = sign, and you got your name and version.  Personally, I'd store it in a Dictionary so that the version number can be grabbed quickly from the name.



Another note Application is great but often, it isn't available (not by default in consoles and not at all available in services).  Try to get in the habit of using this to get the path to yourself (requires reference to System.Reflection):


```
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6)
```


----------

