What's new

Very Easy Updater Tutorial(VB.NET)

  • Thread starter Kenpachi
  • Start date
  • Views 8,416
Kenpachi

Kenpachi

Enthusiast
Messages
411
Reaction score
42
Points
85
Sin$
7
Hello guys/girls! I will be helping you make a updater for your programs made in VB.NET. Doing this will allow your users to get the latest versions of your program without having to keep up with a forum post or going to a website to see if there is a newer version. Let's get this started.



1. Open up your project/start a new project.
Add a new form for the updater and add a button, 5 labels and a progress bar.
Change the button text to Update or whatever you want and set the Enabled property to False
Your form should look like this
406b030f3a0db15584615640dd2046e8.png

Before we continue, you should add this to your to the top of your code
Code:
Imports System.IO
Imports System.Net

Then add this to your code
Code:
Dim locat As String = System.Reflection.Assembly.GetEntryAssembly.Location
    Dim MyDirectory As String = System.IO.Path.GetDirectoryName(locat)
    Public totalsize As String
    Public link As String
    Public Csize As String

You are also going to need to download a file hosting program called Dropbox
https://www.dropbox.com/downloading

Once you have downloaded Dropbox, you will see a blue box icon in the system tray at the bottom right hand of your screen. Double click the icon then open the 'Public' folder. Then I recommend you make a new folder in the Public folder named after your program to keep track of all of your files.

Once in the folder you will use to store your files, add a new text document named Version and make the text in it 1.0.0.0 and save it. Now right click that file and you should see a Dropbox option with some more options within it. Remember this because you will need to click "Copy Public Link" from that menu later in this tutorial.

2. Add a Timer and a BackgroundWorker to your form. Double click both of them to get the events in your code. Now click on the BackgroundWorker_DoWork and on the top right of the code window click the drop down menu with the text "DoWork" and select "RunWorkerComplete" and double click the form and button to get them in the code as well.

3. Now let's got down to the code. I will make this easy and just post the whole thing now and explain things afterwards.
Code:
Imports System.IO
Imports System.Net
 
Public Class Form1
 
    Dim locat As String = System.Reflection.Assembly.GetEntryAssembly.Location
    Dim MyDirectory As String = System.IO.Path.GetDirectoryName(locat)
    Public totalsize As String
    Public link As String
    Public Csize As String
    Public amount As String
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        My.Computer.FileSystem.RenameFile(MyDirectory + "\" + Me.ProductName + ".exe", Me.ProductName + ".old")
        Timer1.Start()
        BackgroundWorker1.RunWorkerAsync()
        Button1.Enabled = False
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If File.Exists(MyDirectory + "\" + Me.ProductName + ".old") Then
            File.Delete(MyDirectory + "\" + Me.ProductName + ".old")
        End If
        Label4.Text = Me.ProductVersion
        Try
            Dim instance As WebClient = New WebClient
            Dim address As String = "[COLOR=#ffff99][B]YOUR VERSION URL[/B][/COLOR]"
            Dim returnValue As String
            returnValue = instance.DownloadString(address)
            Label5.Text = returnValue
            If Label4.Text >= Label5.Text Then
            Else
                Button1.Enabled = True
            End If
        Catch ex As Exception
 
        End Try
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub
 
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Try
            link = "[B]YOUR PROGRAM URL[/B]"
            Dim size1 As Integer
            Dim wr As WebRequest
            wr = WebRequest.Create(link)
            Dim webr As WebResponse = wr.GetResponse
            size1 = webr.ContentLength
            webr.Close()
            size1 = size1 / 1024
            ProgressBar1.Maximum = size1
            totalsize = size1
            My.Computer.Network.DownloadFile("[COLOR=#ffff99][B]YOUR PROGRAM URL[/B][/COLOR]", MyDirectory + "\" + Me.ProductName + ".exe")
        Catch ex As Exception
 
        End Try
    End Sub
 
    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Try
            Shell(MyDirectory + "\" + Me.ProductName + ".exe")
            Me.Close()
        Catch ex As Exception
 
        End Try
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If File.Exists(MyDirectory + "\" + Me.ProductName + ".exe") Then
            Dim o As New FileInfo(MyDirectory + "\" + Me.ProductName + ".exe")
            amount = o.Length
            amount = amount / 1024
            Csize = amount
            ProgressBar1.Value = amount
        End If
        Label1.Text = Csize + " KBs / " + totalsize + " KBs"
    End Sub
End Class

Now go to your Dropbox folder and copy the link and paste it where it says YOUR VERSION URL
As for your program URL, as long as you store your program in the same folder as the version.txt, you can simply modify the version url by replacing the Version.txt with YOURPROGRAM.exe

Here are my URLs
[Click here to view this link]
[Click here to view this link]

Now let's get something straight, this code is for the example form I will provide the source code for in this post. You will probably need to do a few things to get this to work right for your program.

First of all, you will need to add this part of the code to form_load of your main form in your program.
Code:
If File.Exists(MyDirectory + "\" + Me.ProductName + ".old") Then
            File.Delete(MyDirectory + "\" + Me.ProductName + ".old")
        End If

Also, on the BackgroundWorker_RunWorkerComplete, you need to change the code
Code:
Me.Close
to
Code:
Form1.Close
or whatever the name of your main form is.

Now once you have finished your build for your program and it is ready for release, just click the "Debug" menu item on Visual Basic and click Build YOUR PROGRAM NAME and go to your projects folder in My Documents > VB 2010/2008 > Projects > Your program > Your program > Bin > Release

Just copy and paste your program into your Dropbox folder and you are done!

Once you want to release an update to your program just replace the old version of your program in your Dropbox folder with your latest version and then open your version.txt file and change the text to the latest version like change it from 1.0.0.0 to 2.0.0.0.

Let's understand how this works. You replace the files and change the version text. Your users open the updater, it checks the version.txt tex and checks if your current version is greater than or equal to the latest version. If the latest version is greater than your current version, it will enable the button which will allow the user to download your program. The program will then let then download the newer program and will let the user know the progress of the download and close the old version, then open the new version and delete the old version. Viola! You now have an updater for your program!

You can use this information to do many more things to make it even better. I like to add a textbox that has information about the latest version and I add an autoupdater to my programs like xPro.

Source: [Click here to view this link]

I hope this tutorial helps you guys. I made another tutorial a while back but nobody wanted to watch a 24 minute video lol. This is also a better version as well. If you have any questions, feel free to ask!
 
GoldBl4d3

GoldBl4d3

VIP
VIP
Retired
Programmer Mythical Veteran Legendary Veteran
Messages
3,785
Reaction score
1,452
Points
600
Sin$
0
Nice tutorial, but your not really helping anybody learn. People are just going to copy and paste your code. You want them to learn and understand what they are doing.

Maybe remove the code, and replace them for pictures of the code. That way it forces them write each line.
 
Z61

Z61

Some times our saints are sinners
Retired
Programmer Forum Addict Odysseus' Summit
Messages
5,468
Reaction score
3,429
Points
1,042
Sin$
0
I don't understand what you mean. It's all pretty much the same.
I do like your signature though lol Silly little hardcore bands that suck ****.
Most languages use a C style syntax, while Basic uses it's awful syntax.
 
Kenpachi

Kenpachi

Enthusiast
Messages
411
Reaction score
42
Points
85
Sin$
7
Nice tutorial, but your not really helping anybody learn. People are just going to copy and paste your code. You want them to learn and understand what they are doing.

Maybe remove the code, and replace them for pictures of the code. That way it forces them write each line.
Good idea. I may do this after work but they could still just copypasta from the source code.
 
Z61

Z61

Some times our saints are sinners
Retired
Programmer Forum Addict Odysseus' Summit
Messages
5,468
Reaction score
3,429
Points
1,042
Sin$
0
Good idea. I may do this after work but they could still just copypasta from the source code.
It's not really a copy and paste if they have to write it, which would help them learn better.
 
Kenpachi

Kenpachi

Enthusiast
Messages
411
Reaction score
42
Points
85
Sin$
7
It's not really a copy and paste if they have to write it, which would help them learn better.
I would have to remove the DL link for the source at the bottom or else they could just download that and copypasta is what I am getting at.
 
Top Bottom
Login
Register