Creating an File Update Checker Tool #2 Versions
Submitted by GeePee on Saturday, March 28, 2015 - 22:45.
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...
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...
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...
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...
If the version found is higher than the one already found then we update the text file & versionLabel text and alert a message box.
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- pathText.ReadOnly = True
- pathText.Text = My.Settings.Path
- End Sub
- Private Sub browseButton_Click(sender As Object, e As EventArgs) Handles browseButton.Click
- Dim fo As New OpenFileDialog
- fo.Filter = "Text Files | *.txt"
- fo.Multiselect = False
- fo.ShowDialog()
- My.Settings.Path = fo.FileName
- My.Settings.Save()
- pathText.Text = My.Settings.Path
- End Sub
- Using sr As New StreamReader(pathText.Text)
- While (sr.Peek <> -1)
- Dim line As String = sr.ReadLine()
- If (line.ToLower().StartsWith("version:")) Then
- Dim splits As String() = line.Split(":")
- versionLabel.Text = splits(1)
- End If
- End While
- End Using
- If (versionLabel.Text = Nothing) Then
- versionLabel.Text = "-1"
- End If
- doUpdate()
- Private Function doUpdate()
- Dim episode As Integer
- Dim src As String = getSource("http://www.bbc.co.uk/iplayer/search?q=The%20Call%20Centre")
- Dim options As String() = GetBetweenAll(src, "<ul class=""result-list listview episodelist"">", "<p class=""result-count"">")
- If (options.Count > 0) Then
- For Each opt As String In options
- statusLabel.Text = "Processing new option..."
- Dim title As String = GetBetween(opt, "The Call Centre - Series 2<br /><span>", "</span>")
- statusLabel.Text = "Found Program, Processing..."
- episode = Val(title(0))
- Dim previousEpisode As Integer = Nothing
- If (versionLabel.Text = "Unfound.") Then
- previousEpisode = -1
- Else
- previousEpisode = Val(versionLabel.Text)
- End If
- If (episode > previousEpisode) Then
- versionLabel.Text = episode.ToString()
- statusLabel.Text = "Found new episode, " + episode.ToString()
- MsgBox("Found new episode, " + episode.ToString())
- Dim lines As List(Of String) = New List(Of String)
- Using sr As New StreamReader(pathText.Text)
- While (sr.Peek <> -1)
- lines.Add(sr.ReadLine())
- End While
- End Using
- Dim wroteLine As Boolean = False
- Using sw As New StreamWriter(pathText.Text)
- For Each line As String In lines
- If (line.ToLower().StartsWith("version:")) Then
- sw.WriteLine("version:" + episode.ToString())
- wroteLine = True
- Else : sw.WriteLine(line)
- End If
- Next
- If Not (wroteLine) Then sw.WriteLine("version:" + episode.ToString())
- End Using
- End If
- Next
- statusLabel.Text = "Idle..."
- If (episode >= 0) Then
- versionLabel.Text = episode
- Else : versionLabel.Text = "Unfound."
- End If
- End If
- End Function
Add new comment
- 12 views