Creating an File Update Checker Tool #2 Versions

Introduction: This tutorial is on how to create an update checker tool. Note; this is not a software/application self updating feature, this is to check whether there are new versions of another program/video/audio file ready to view. Purpose: I am going to be making this program for BBC iPlayer so I can check if there are new episodes of certain TV Shows I watch. You can alter this program to any other purpose with minor tweaking of source code. Path: We are going to store the path in a settings key, so to create this go to; Project > {Project Name} Properties > Settings > Then rename the value key 'name' from 'setting' to 'Path'. Now on form load we want to set the textbox 'pathText' text to the path held within the settings. Also set the textbox text to read only...
  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2.         pathText.ReadOnly = True
  3.         pathText.Text = My.Settings.Path
  4. End Sub
Next we want to create the browser action. First we create an openfiledialog to allow the user to select files, then we set the filter to text only files, disable multi file selecting and show the dialog. Finally we set the file path in settings to the newly selected path then update the textbox text's path...
  1. Private Sub browseButton_Click(sender As Object, e As EventArgs) Handles browseButton.Click
  2.         Dim fo As New OpenFileDialog
  3.         fo.Filter = "Text Files | *.txt"
  4.         fo.Multiselect = False
  5.         fo.ShowDialog()
  6.         My.Settings.Path = fo.FileName
  7.         My.Settings.Save()
  8.         pathText.Text = My.Settings.Path
  9. End Sub
Checking Latest Version: Now for the updating part. Once we have set the file path we want to get the latest found version held within the file. To do this we open a new reading file stream to the path and read the line beginning with "version:", we then split the line by ":" to get separate the text 'version' and the other side of the colon (':'). We then set the text of the versionLabel to the version side of the 'version:' line...
  1. Using sr As New StreamReader(pathText.Text)
  2.         While (sr.Peek <> -1)
  3.                 Dim line As String = sr.ReadLine()
  4.                 If (line.ToLower().StartsWith("version:")) Then
  5.                         Dim splits As String() = line.Split(":")
  6.                         versionLabel.Text = splits(1)
  7.                 End If
  8.         End While
  9. End Using
  10. If (versionLabel.Text = Nothing) Then
  11.         versionLabel.Text = "-1"
  12. End If
Once we have the current version, we run a function named 'doUpdate' by calling it. This function will get the search results page source of the appropriate page containing the latest version of the file to check, extract the latest version and compare it with the one set as the 'versionLabel' text...
  1. doUpdate()
  1. Private Function doUpdate()
  2.         Dim episode As Integer
  3.         Dim src As String = getSource("http://www.bbc.co.uk/iplayer/search?q=The%20Call%20Centre")
  4.         Dim options As String() = GetBetweenAll(src, "<ul class=""result-list listview episodelist"">", "<p class=""result-count"">")
  5.         If (options.Count > 0) Then
  6.             For Each opt As String In options
  7.                 statusLabel.Text = "Processing new option..."
  8.                 Dim title As String = GetBetween(opt, "The Call Centre - Series 2<br /><span>", "</span>")
  9.                 statusLabel.Text = "Found Program, Processing..."
  10.                 episode = Val(title(0))
  11.                 Dim previousEpisode As Integer = Nothing
  12.                 If (versionLabel.Text = "Unfound.") Then
  13.                     previousEpisode = -1
  14.                 Else
  15.                     previousEpisode = Val(versionLabel.Text)
  16.                 End If
  17.                 If (episode > previousEpisode) Then
  18.                         versionLabel.Text = episode.ToString()
  19.                     statusLabel.Text = "Found new episode, " + episode.ToString()
  20.                     MsgBox("Found new episode, " + episode.ToString())
  21.                     Dim lines As List(Of String) = New List(Of String)
  22.                     Using sr As New StreamReader(pathText.Text)
  23.                         While (sr.Peek <> -1)
  24.                             lines.Add(sr.ReadLine())
  25.                         End While
  26.                     End Using
  27.                     Dim wroteLine As Boolean = False
  28.                     Using sw As New StreamWriter(pathText.Text)
  29.                         For Each line As String In lines
  30.                             If (line.ToLower().StartsWith("version:")) Then
  31.                                 sw.WriteLine("version:" + episode.ToString())
  32.                                 wroteLine = True
  33.                             Else : sw.WriteLine(line)
  34.                             End If
  35.                         Next
  36.                         If Not (wroteLine) Then sw.WriteLine("version:" + episode.ToString())
  37.                     End Using
  38.                 End If
  39.             Next
  40.             statusLabel.Text = "Idle..."
  41.             If (episode >= 0) Then
  42.                 versionLabel.Text = episode
  43.                         Else : versionLabel.Text = "Unfound."
  44.                 End If
  45.         End If
  46. End Function
If the version found is higher than the one already found then we update the text file & versionLabel text and alert a message box.

Add new comment