Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

How to make your own updater for your program.

Please don't down me if you do not like this, I accept constructive criticism and use that to my advantage, But anyways I'd appreciate if you don't like this TuT or me please don't down me, Thanks. Now on to the tut :D

At the bottom of the post is my Youtube Video Tutorial for this.

Things you need:
1) A webhost. Free ones are perfect as all you need it for is to host 2 text files directly. I use www.x10hosting.com its free and simple to sign up on.
2) VisualBasic Express 2010 or any version really. I just prefer 2010, or 2013

Now I am going to try to quickly as I can explain how my updater works.() The updater finds the the current version(Version of program on the computer) from a text document(Version.txt) that your program creates with only the number of the version of the program. Your program will update this Version.txt with it's version every time it runs, The updater stores the current version as a variable for later use. Then the updater finds the Newest Version(The most recent version you have for download) from a text file(Ver.txt) hosted online(That is why you need #1 in 'Things you need') Ver.txt only contains the newest version number and the updater gets that number and stores it as another variable. Now that is has both the current version and the newest version when the user clicks the update button in the Updater. Once the button is clicked it will go through a If statement comparing the versions. It checks if the Newest is Greater than the Current number and if it is it continues with the update process and if isn't it displays a messagebox pops up telling the user that no update is needed. So that is in as short as I can make it explanation of what My updater does. On with the tutorial on how to make it.



First I'm going to tell you how I include the Updater in a program. Now I'm Just going to make it a simple form with nothing(Edit: simply just added a label to let me know what its doing, you will see a pic and see it in the code) but the update code, just using it as a example on how to use the updater in your program. There are also plenty of other ways to include it, just play around with it if you want this is just how I do it.

Pic:
[Image: tzDej5G.png]
You can also view this code with highlighted syntax at pastebin: http://adf.ly/wcmzs
Code
Imports System.IO

Public Class Form1


    'These Subs are used in the program that the Updater will be Updating




    Public Shared MyVersion As String = 1 'The version of the program, Used to detect either to update or not in the Updater exe
    Public Shared Dir As String = Directory.GetCurrentDirectory() 'Variable storing the current directory

    Sub UpdateMe()
        'Button to close program and launch the updater
        Application.DoEvents()
        Label1.Text = "opening updater"
        MessageBox.Show("This program will now close and launch the Updater")
        Process.Start(Dir + "\Update.exe")
        Me.Close()
    End Sub
    Public Sub VersionTextFile()
        'Checks if the program needs to Create Version.txt
        'or if it already exists and it can continue to
        'write the version number to the text file
        Application.DoEvents()
        Dim FILE_NAME As String = Dir + "\Version.txt"
        If System.IO.File.Exists(FILE_NAME) = True Then
            Label1.Text = "txt file exists"
            Application.DoEvents()
            Label1.Text = "Carry on sir"
            WriteVersionText()
        Else
            Application.DoEvents()
            Label1.Text = "no txt file there"
            File.Create(FILE_NAME).Dispose()
            Label1.Text = "now there is"
            WriteVersionText()
        End If
    End Sub
    Public Sub AdminCheck()
        'I include this in my program for 2 reasons, some
        'of the Functions it has require admin and so
        'no matter where the program is located it will
        'still be able to write the text file
        Application.DoEvents()
        If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) Then
            Label1.Text = "Admin Check Finished"
        Else
            MsgBox("Must Run Program As Admin To Run Properly")
            Me.Close()
        End If
    End Sub
    Public Sub WriteVersionText()
        'This simply writes the version number to the text file
        Application.DoEvents()
        Label1.Text = "Writing Version"
        Dim objReader As StreamWriter
        objReader = New StreamWriter(Dir + "\Version.txt")
        objReader.Write(MyVersion)
        objReader.Close()
    End Sub

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

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

    End Sub
End Class



Next is the updater itself,
Here is a list of everything you need in the form, some things need names, and it has to be put in certain places for it to look right, bellow you can see a example on how everything is:
4 Labels
1 listbox
1 Textbox
1 Button

and here is the pic containing the positions & names of everything you need, basically a diagram or whatever for the Updater:
[Image: NsGI3VW.png]

And here is the code:
or highlighted syntax pastebin post: http://adf.ly/wcn9J
Code
Imports System.IO
Imports System.Net

Public Class Form1

    Private WithEvents httpclient As WebClient
    'Variable Containing The Current Directory
    Public Shared Dir As String = Directory.GetCurrentDirectory()
    'Variable Containing The Directory To Version.txt
    Public Shared VerDir As String = (Dir + "\Version.txt")
    'Variable For Storing The Current Version
    Public Shared Version As String = 0
    'Variable For Storing The NewestVersion
    Public Shared NewVersion As Integer = 0
    'Variable For The .exe That Gets Updated, You Can Make This Changable
    'By Loading A Text File Online With The Exe's Name, Just like
    'We Do To Get The Download URL And The Newest Version
    Public Shared CurrentEXEs As String = "SuicideBooter.exe"
    'Variable For The URL With The Newest Version Linking The Text File(Needs to Be Direct)
    Public Shared VerAddress As String = "http://gethelp.x10host.com/Ver.txt"
    'Variable For The URL With The Download URL Linking The Text File(Needs to Be Direct)
    Public Shared UpdateDL As String = "http://gethelp.x10host.com/UpdateDL.txt"
    'Variable For Storing The Update URL
    Public Shared UpdateURL As String = " "
    'Variable For Retrieving The Newest Version
    Public Shared ReturnValue As String = " "
    'Variable For A WebClient
    Public Shared instance As WebClient = New WebClient



    Sub UpdateMe()
        CheckVer()
        Updater()
    End Sub

    Public Sub AdminCheck()
        'I use this for the same reasons I use it in the program part
        If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) Then
        Else
            MsgBox("Must Run Program As Admin To Run Properly")
            Me.Close()
        End If
    End Sub

    Sub Updater()
        'This sub determines if a update is required or not
        Dim V1 As Integer
        Dim V2 As Integer
        V1 = Version
        V2 = NewVersion
        'Checking if the newest version is greater than the current version
        If V2 > V1 Then
            'If it is greater the program will show a message box then continue the update process
            MessageBox.Show("Update Required")
            GetUpdate()
            ClearDir()
            DownloadUpdate()
        Else
            'if it isnt greater the program will show a message box telling the user no update is needed
            MessageBox.Show("No updating needed, You already have version: " + Version + " Which is the newest version released.")
        End If
    End Sub

    Sub GetVer()
        'This sub will get the current version from Version.txt
        Application.DoEvents()
        'Checking if Version.txt exists
        If My.Computer.FileSystem.FileExists(VerDir) Then
            Dim lines() As String = IO.File.ReadAllLines(VerDir)
            VerB.Items.AddRange(lines)
            Version = VerB.Items(0)
            One.Text = VerB.Items(0)
        Else
            'if it doesn't exists a message box will appear telling the user to run the program at least
            'once before trying to update
            MessageBox.Show("Version file not found please run SuicideBooter at least once before updating!")
        End If
    End Sub

    Sub UpdateMsgBox()
        MessageBox.Show("After pressing the 'OK' button the Update process will start, Please be patient and allow 2-3 mins to finish")
    End Sub

    Sub CheckVer()
        'This sub gets the newest version from the online host (Ver.txt)
        Application.DoEvents()
        Dim instance As WebClient = New WebClient
        Dim address As String = VerAddress
        Dim returnValue As String
        'Changes the returnValue variable to the newest version
        returnValue = instance.DownloadString(address)
        'uses that variable to set the NewVersion variable and Two.Text And Change the StatusT.Text
        NewVersion = returnValue
        Two.Text = returnValue
        StatusT.Text = ("New Version= " + returnValue)
        Refresh()
    End Sub

    Sub GetDLURL()
        'This Sub Grabs The Download URM from the online host (UpdateDL.txt)
        Application.DoEvents()
        Dim instance As WebClient = New WebClient
        Dim address As String = UpdateDL
        Dim returnValue As String
        returnValue = instance.DownloadString(address)
        'Uses the returnValue variable to Change the UpdateURL Variable to The Download link
        UpdateURL = returnValue
        StatusT.Text = ("Downloading Update From " + returnValue)
        Refresh()
    End Sub
    Sub DLUpdate()
        'This simply just downloads the program, and puts in in the current directory and rename it
        Application.DoEvents()
        My.Computer.Network.DownloadFile(UpdateURL, (Dir + "\SuicideBooter.exe"))
    End Sub


    Sub DownloadUpdate()
        GetDLURL()
        DLUpdate()
        UpdateFinished()
    End Sub

    Sub UpdateFinished()
        'Just displays a message box letting user know update is finished
        MessageBox.Show("Update Finished, You may now run SuicideBooter!")
    End Sub

    Sub GetUpdate()
        UpdateMsgBox()
    End Sub

    Sub ClearDir()
        'This deletes the Files that are being updated from Directory to allow updating
        Dim File1 As String
        Dim File2 As String
        File1 = (Dir + "\SuicideBooter.exe")
        File2 = (Dir + "\Version.txt")
        If System.IO.File.Exists(File1) = True Then
            System.IO.File.Delete(File1)
        End If
        If System.IO.File.Exists(File2) = True Then
            System.IO.File.Delete(File2)
        End If
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AdminCheck()
        GetVer()
        CheckVer()
    End Sub







#Region "Form Buttons"

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











End Class

And you can easily play around with the code to make it more suitable to your needs. Hope you guys find this useful.





And also here is the Visual Basic Express 2010 Source Code:
Program Part: http://adf.ly/wcnNI
Updater Part: http://adf.ly/wcnOT
quit being a Gay fish lol..... good novice tut can tell you tried so hard to make it presentable skimmed through it

Code
If System.IO.File.Exists(File1) = True Then
      System.IO.File.Delete(File1)
      End If
      If System.IO.File.Exists(File2) = True Then
      System.IO.File.Delete(File2)
      End If

that used to raise red flags in a lot of applications when updating

also mentioning that ClickOnce does actually exists with this sole intent :)

keep it up

B-)
{ Vi Veri Universum Vivus Vici }
(Jan 24, 2015 10:09 PM)#Kat Wrote:  quit being a Gay fish lol..... good novice tut can tell you tried so hard to make it presentable skimmed through it

Code
If System.IO.File.Exists(File1) = True Then
      System.IO.File.Delete(File1)
      End If
      If System.IO.File.Exists(File2) = True Then
      System.IO.File.Delete(File2)
      End If

that used to raise red flags in a lot of applications when updating

also mentioning that ClickOnce does actually exists with this sole intent :)

keep it up

B-)

Thanks for your feedback man, I know it's not the best program out there but I did work real hard on it like you said, and I'm still learning so you will probably see me posting new programs that are better and more functional soon. Seems like though that every program that I work really hard on, it seems like the next one I code is better than the last, So that means I'm learning some more each program I work on!
Thanks man ! this will help me a lot !
Pretty cool man! I'm sure i someday will check this out. Thanks!
Things I want to mention:
  • Take adfly out of those download links. Referrals don't belong outside of the referral section.
  • You type "MyVersion" as string but initialize with an integer. They don't change at runtime and so shouldn't be variables.
  • Application.DoEvents() everywhere. Really?
  • Addition operator for concatenation? Sometimes VB is too lax, use ampersand instead.
  • Shared? Do you know the purpose of Shared?
  • As a general rule, software should always work on lowest account privileges, avoiding a UAC prompt whenever possible. Out of all the updaters I've seen here, none required admin. Even at that, you shut it down if not admin?!
  • Using an external document to save the version. No need, there's something called Get/SaveSetting (or My.Settings, if you like).
  • In GetVer, you check if the file exists (which you shouldn't be doing anyway) but if it doesn't, you tell them to run "Suicide Booter" at least once more... Unbelievable. You handle the check, so create the file - even though you shouldn't be using a file to hold like I just said.
  • Considering the last point, a listbox to temporarily hold version info... What.
  • You don't need a sub for prompting a message box.
  • Sub to call other subs.
  • Global variables everywhere. Why?
  • Control names. The hell is VerB? VersionBox? Label "One". Eugh.
I'm afraid to dig any deeper. Of course you put effort into this tutorial and I appreciate that, but too many big bad no-nos here.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by KyFuJan 10, 2019 07:35 AM 6 304 Jan 10, 2019 07:35 AM
Last Post: InterGalactic
[.NET] by SoldiercrimesDec 31, 2017 04:16 AM 10 636 Dec 31, 2017 04:16 AM
Last Post: Hopsin
[Python] by xx1ben99xxNov 20, 2016 10:41 AM 2 153 Nov 20, 2016 10:41 AM
Last Post: xx1ben99xx