Automatic Updates

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
108 posts Page 1 of 11
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Automatic Updates
CodenStuff
Hello,

I am making this tutorial because it was a feature requested by a user. This tutorial will let you add a new function to your application that will allow to automatically detect application updates and then download and install them automatically.

Before I begin I have to tell you that in order for this to work you do need a webhost because its pretty much the only way I know of that can allow you to do automatic updates. But dont worry if you dont have a webhost because you can use google sites which is free and I have tested it with this tutorial and it all works great cooll; . So sign up for a google sites account and then you can continue with the rest of the tutorial.

All set up?, great lets get started.

This part of the tutorial is the bit that you need to add into the application you want to update automatically. Go into Project > Application Properties > Settings and add the following setting and its value

version - value = ver1

ver1 means that this is version 1 of your application. When you make an updated version of your application you need to change that value to ver2 and so on and so on for every new version you make.

Make sure you have these imports at the top of your code:

Imports System.Net
Imports System.Net.WebClient
Imports System.IO


Now in your applications startup event you need to add this code rite at the very top before anything else:
Code: Select all
Dim url As String = "http://www.YOURSITE.com/version.html"
        Dim pageRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
        Dim pageResponse As WebResponse = pageRequest.GetResponse()
        Dim page As String = ""
        Using r As New StreamReader(pageResponse.GetResponseStream())
            page = r.ReadToEnd()
        End Using
        If Not page = Convert.ToString(My.Settings.version) Then
            System.Diagnostics.Process.Start(Application.StartupPath & "\UpdateChecker.exe")
            Me.Close()
        End If
    End Sub
Thats all you have to do to enable it into your application. Now we move onto a completely new project, its realy short dont worry.

Start a new project and call it "UpdateChecker".

Now add a ProgressBar control to the form and a Timer control. Set the timer interval to "500" and leave it disabled. You can customize the form as you wish to match the look of your main application if you want but I recommend you keep the form small because its only for updating your application and will only be on screen for a few moments.

Now once you have added those controls you can simply override all the project code with the code below:
Code: Select all
Imports System.Net.webclient
Imports System.IO
Imports System.net
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim url As String = "http://www.YOURSITE.com/updatedownload.html"
        Dim pageRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
        Dim pageResponse As WebResponse = pageRequest.GetResponse()
        Dim page As String = ""
        Using r As New StreamReader(pageResponse.GetResponseStream())
            page = r.ReadToEnd()
        End Using
        Dim WebUpdate As System.Net.WebClient
        WebUpdate = New System.Net.WebClient()
        AddHandler WebUpdate.DownloadProgressChanged, AddressOf OnDownloadProgressChanged
        AddHandler WebUpdate.DownloadFileCompleted, AddressOf OnFileDownloadCompleted
        WebUpdate.DownloadFileAsync(New Uri(page), Application.StartupPath & "\Updated.exe")
    End Sub
    Private Sub OnDownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
        Dim totalSize As Long = e.TotalBytesToReceive
        Dim downloadedBytes As Long = e.BytesReceived
        Dim percentage As Integer = e.ProgressPercentage
        ProgressBar1.Increment(percentage)
    End Sub
    Private Sub OnFileDownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
        If e.Cancelled Then
        ElseIf Not e.Error Is Nothing Then
            MsgBox("There was a problem downloading the update,  please try again later.")
        Else
            If My.Computer.FileSystem.FileExists(Application.StartupPath & "\MyApplicationName.exe") Then
                My.Computer.FileSystem.DeleteFile(Application.StartupPath & "\MyApplicationName.exe", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
            End If
            If My.Computer.FileSystem.FileExists(Application.StartupPath & "\Updated.exe") Then
                My.Computer.FileSystem.RenameFile(Application.StartupPath & "\Updated.exe", "MyApplicationName.exe")
            End If
            Timer1.Enabled = True
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\MyApplicationName.exe") Then
            System.Diagnostics.Process.Start(Application.StartupPath & "\MyApplicationName.exe")
            Me.Close()
        End If
    End Sub
End Class
There are a couple of things you need to change in here so I will explains those to you now. You need to change the following URL to your websites URL:

Dim url As String = "http://www.YOURSITE.com/updatedownload.html"

And you need to change the word "MyApplicationName.exe" to the name of your application, this occurs 5 times so you need to change them all. Once you are happy you have done everything just save and build the project and copy the "UpdateChecker.exe" into your main applications folder.

OK thats all the coding done so save>build and close VB. Now we need to make 2 files. The first file is called "version.html" and inside this file you need to type ver1 and upload it to your webhost. This file tells your application if it is the latest version or not.

Now whenever you make an update to your application you first need to change the text inside "version.html" to the next version number for example ver2. Then you need to upload your new updated application to your webhost and create the next file....

Second file is "updatedownload.html" and inside this file you need to put the download link to your updated application, so inside this file you would write something like "http://www.YOURSITE.com/MyApplication.exe" and then upload it to your webhost.

You can download the UpdateChecker source-code here if you want:
UpdateCheckerv1.zip
If you need any help please let me know, I tried to explain it as clearly as possible but it does sound a little bit confusing especially the files part at the end. If you get stuck please let me know and I will help you.

Happy coding cooll;
You do not have the required permissions to view the files attached to this post.
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Robby
VIP - Donator
VIP - Donator
Posts: 417
Joined: Mon Aug 03, 2009 4:01 am

Re: Automatic Updates
Robby
hi codenstuff

thx for making a tutorial on this but i dont understand it a little.

I have made a form 1 as a start up form also the form of my application...
I made a form 7 which is for the updater....
I dont know where to put the first code before starting another project

is it main form or the update form?

and also i m using google sites in this as u recommended but its comiong along no good as of i dont know how to upload a page as a page rather than making a new page and then ataching the file to it.. so please help of that and thats all i need to know the tutorial seems great as i read the code a bit and it seems to match what i was looking for

ty and regards,

Robby1998
My current Projects Listed Below:
Toixt++ Text Editor Just 10 or less more features to go!
Image
Image
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Re: Automatic Updates
CodenStuff
Hello robby1998,

OK if you double click on your main form it should go into code-view and create the form_load event sub. Inside this event is where you need to use the first piece of code. Dont forget to create the "version" setting in your project properties though, its very important.

Then you can start a new project to make the UpdateChecker. Once you have made this you need to copy the UpdateChecker.exe into your main projects folder (the one you added the update code to).

In order to put your files onto google sites you need to first login and click on your site then you should see a screen that looks like this:

Image

Click "NewPage" button and then choose "File Cabinet", give it a name and click "Create Page".

Now you should see your new page and a button called "Add File" click on this and you can upload the .html files that you need to make. Once you have finished uploading the 2 html files if you hover your mouse over them and then right-click your mouse and choose copy shortcut you should get links that look something like this:

http://sites.google.com/site/mysite/newcab/version.html?attredirects=0
http://sites.google.com/site/mysite/newcab/updatedownload.html?attredirects=0


You need to make a small change to these links and remove the "?attredirects=0" so they look like this:

http://sites.google.com/site/mysite/newcab/version.html
http://sites.google.com/site/mysite/newcab/updatedownload.html


The "version.html" link goes into your main application code that you used in the Form_Load event. And the second link "updatedownload.html" goes into the "UpdateChecker" project code.

When you are ready to upload your updated application you can get its link in the same way and copy it into the "updatedownload.html" file.

Hope that helps cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Robby
VIP - Donator
VIP - Donator
Posts: 417
Joined: Mon Aug 03, 2009 4:01 am

Re: Automatic Updates
Robby
o sorry for not replying and i added this feature but i dont think it is working or something so i will leave the code in their for now and i will try it again or i might have put the version.html or updatedownload.html on somewhere else :(

Regards,

Robby1998
My current Projects Listed Below:
Toixt++ Text Editor Just 10 or less more features to go!
Image
Image
User avatar
CodemaN
VIP Access - Denied
VIP Access - Denied
Posts: 74
Joined: Fri Sep 18, 2009 3:18 pm

Re: Automatic Updates
CodemaN
thanks very cool program
User avatar
AleRi8
Top Poster
Top Poster
Posts: 153
Joined: Sun Sep 20, 2009 2:30 pm

Re: Automatic Updates
AleRi8
Hi Thanks For The Program But I Have Two Errors When I Paste The First Lot Of Codes on the main application but if i put end sub then the error goes but then the rest of the startup code goes wrong

http://i131.photobucket.com/albums/p317 ... /11000.png
check out my software
win7 itweaker basic £4.50
viewtopic.php?f=70&t=793
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Re: Automatic Updates
CodenStuff
Hello AleRi8,

You need to add a richtextbox control to your form (you can make it invisible if you want). I shouldnt have put an "End Sub" in the first part sorry.

But this code inside the "Form_Load" event does work. To help you your "Mainform_Load" event should look like this:
Code: Select all
   Private Sub Mainform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim url As String = "http://www.YOURSITE.com/version.html"
        Dim pageRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
        Dim pageResponse As WebResponse = pageRequest.GetResponse()
        Dim page As String = ""
        Using r As New StreamReader(pageResponse.GetResponseStream())
            page = r.ReadToEnd()
            RichTextBox1.Text = page
        End Using
        If page = Convert.ToString(My.Settings.version) Then
            System.Diagnostics.Process.Start(Application.StartupPath & "\UpdateChecker.exe")
            Me.Close()
        End If
        'THE REST OF YOUR CODE SHOULD GO HERE
        '''''''''''''''''''''''''''''''''''''
    End Sub
Hope that helps cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
AleRi8
Top Poster
Top Poster
Posts: 153
Joined: Sun Sep 20, 2009 2:30 pm

Re: Automatic Updates
AleRi8
Ok Thanks For Your Help
check out my software
win7 itweaker basic £4.50
viewtopic.php?f=70&t=793
User avatar
tigerbaddad
New Member
New Member
Posts: 21
Joined: Fri Aug 28, 2009 11:57 am

Re: Automatic Updates
tigerbaddad
Image




yes , thanks
Last edited by tigerbaddad on Wed Oct 07, 2009 6:00 pm, edited 1 time in total.
__+) make by @hashem (+___
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Re: Automatic Updates
CodenStuff
Hello,

Why did you post that image?. :?

If its related to this tutorial then its saying that the webbrowsers URL is empty, which probably means that you have not put a download link into the "updatedownload.html" file that you uploaded to your webhost.

Thank you.
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
108 posts Page 1 of 11
Return to “Tutorials”