Post: Application Updater - Source - One-line setup
06-01-2012, 12:29 PM #1
fill0botto95
You talkin to me?
(adsbygoogle = window.adsbygoogle || []).push({}); One of my friend asked me to make an updater for him. Well, even if there are a lot of sources about this on the net, I think I did a pretty good job so I'm giving this out as open source.


Credits also to idb who shared his CountOccurrence function with the community.


Sample:
You must login or register to view this content.


Usage:
You must login or register to view this content.


The .txt file must follow this structure:


    (last product version)|(lastest version doanload link)|(optional: changelogs, putting a "/" equals to a new line)



Code:
    
Class Updater


Private DownloadLink As String = String.Empty
Private Bar As New ProgressBar With {.Location = New Point(10, 180), .Height = 15, .Width = 320}


#Region "Forms"


Delegate Sub ChangeFormStatus(ByVal F As Form, ByVal Var As Boolean)
Sub ChangeStatus(ByVal Var As Boolean)
For Each F As Form In My.Application.OpenForms
F.Invoke(New ChangeFormStatus(AddressOf SingleFormChange), New Object() {F, Var})
Next
End Sub
Sub SingleFormChange(ByVal F As Form, ByVal Var As Boolean)
If Var Then
F.Show()
Else
F.Hide()
End If
End Sub


#End Region


Sub New(ByVal Link As String, ByVal ForceUpdate As Boolean)
Dim T As New Threading.Thread(Sub() CheckUpdates(Link, ForceUpdate))
T.Start()
End Sub


Sub CheckUpdates(ByVal Link As String, ByVal ForceUpdate As Boolean)
ChangeStatus(False)
If Not System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() Then : Exit Sub : End If
Dim Version = String.Empty, Changelog As String = String.Empty


Dim resp As String = New Net.WebClient().DownloadString(Link)
Version = resp.Split("|")(0)
DownloadLink = resp.Split("|")(1)
If CountOccurance(resp, "|", True) > 1 Then : Changelog = resp.Split("|")(2) : End If


If Version = Application.ProductVersion.ToString Then
ChangeStatus(True)
Else
If ForceUpdate Then
CreateForm(Changelog)
Else
Select Case MessageBox.Show("A new updated version is available, would you like to update now?", "Update Available", MessageBoxButtons.YesNoCancel)
Case DialogResult.Yes
CreateForm(Changelog)
Case DialogResult.No
ChangeStatus(True)
Case DialogResult.Cancel
End
End Select
End If


End If
End Sub


Function CountOccurance(ByVal Data As String, ByVal Search As String, Optional ByVal CaseSensitive As Boolean = False) As Integer
'credits to idb
Return (IIf(CaseSensitive, (Data.Length - (Data.Replace(Search, "").Length)), (Data.Length - (Data.ToLower.Replace(Search.ToLower, "").Length))) / Search.Length)
End Function


#Region "Form Processes"


Private Sub CreateForm(ByVal Changelog As String)
Dim Frm As New Form With {.Size = New Size(350, 240), .Text = "Updating...", .StartPosition = FormStartPosition.CenterScreen, .FormBorderStyle = FormBorderStyle.FixedDialog}
Frm.Controls.Add(New Label With {.Text = "The process will take few seconds...", .Location = New Point(10, 20), .Width = 270})
Frm.Controls.Add(New Label With {.Text = "Changelogs:", .Location = New Point(10, 43), .Width = 100, .Height = 15})
Frm.Controls.Add(New TextBox With {.Multiline = True, .ScrollBars = ScrollBars.Vertical, .Location = New Point(10, 65), .Height = 100, .Width = 320, .Enabled = False, .Text = IIf(String.IsNullOrWhiteSpace(Changelog), Nothing, Changelog.Replace("/", vbNewLine))})
Frm.Controls.Add(Bar)
AddHandler Frm.Load, AddressOf LoadDownloadForm
AddHandler Frm.FormClosing, AddressOf FormClosing
Application.Run(Frm)
End Sub


Private Sub LoadDownloadForm()
Dim w As New Net.WebClient
AddHandler w.DownloadProgressChanged, AddressOf ProgressChanged
AddHandler w.DownloadFileCompleted, AddressOf DownloadCompleted
w.DownloadFileAsync(New Uri(DownloadLink), Application.StartupPath & "\" & IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath) & "Updated.exe")
End Sub


Private Sub ProgressChanged(ByVal sender As Object, ByVal e As Net.DownloadProgressChangedEventArgs)
Dim bytesScaricati As Double = Double.Parse(e.BytesReceived.ToString())
Dim bytesTotali As Double = Double.Parse(e.TotalBytesToReceive.ToString())
Dim percentage As Double = (bytesScaricati / bytesTotali * 100)
Bar.Value = Int32.Parse(Math.Truncate(percentage).ToString())
End Sub


Private Sub DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
Process.Start(Application.StartupPath & "\" & IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath) & "Updated.exe")
Dim Close As New ProcessStartInfo With {.FileName = "cmd.exe", .WindowStyle = ProcessWindowStyle.Hidden}
Close.Arguments = "/C ping 1.1.1.1 -n 1 -w 2000 > Nul & Del """ & Application.ExecutablePath & """"
Process.Start(Close)
End
End Sub


Private Sub FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
e.Cancel = True
End Sub


#End Region


End Class
(adsbygoogle = window.adsbygoogle || []).push({});
06-01-2012, 03:06 PM #2
Pichu
RIP PICHU.
Nicely done, I did this to check an XML file on my host and if the application build number was less than that in the XML file, then it would prompt the user to update.

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo