Wednesday, November 29, 2006

Version Checking Class for PAD files.

Last Night I wrote what I thougt was some really brilliant code for checking a PAD file for the proper version. For my software I keep a pad file online which allows me to simply upload the PAD file for my software to another site. It also allows me to use the same file to check the version in my software. Here is the class that I use to do this. Calling it is really simple, just invoke the method with the current version and the URL to the pad file. If there is no internet access or an error, it returns that there is no update available.





Imports System.io
Imports System.Net
Imports System.Text
Imports System.Xml
Imports System.Xml.XmlDocument
Public Class clsCheckVersion
Private strListedVersion As String
Private strDownloadURL As String
Private strApplicationInfoURL As String
Public ReadOnly Property ListedVersion() As String
Get
Return strListedVersion
End Get
End Property
Public ReadOnly Property PrimaryDownloadURL() As String
Get
Return strDownloadURL
End Get
End Property
Public ReadOnly Property ApplicationInfoURL() As String
Get
Return strApplicationInfoURL
End Get
End Property
Public Function CheckVersion(ByVal strCurrentVersion As String, ByVal strPadFile As String) As Boolean
Try
Dim xmlPad As New Xml.XmlDocument
Dim xmlNode As XmlNode
Dim strXMLFileName As String
strXMLFileName = GetURL(strPadFile)
xmlPad.Load(strXMLFileName)

' Primary Download URL (not required)
Try
xmlNode = xmlPad.SelectSingleNode("/XML_DIZ_INFO/Web_Info/Download_URLs/Primary_Download_URL")
strDownloadURL = xmlNode.InnerText
Catch ex As Exception
strDownloadURL = ""
End Try

' Primary Application info URL (Not Required)
Try
xmlNode = xmlPad.SelectSingleNode("/XML_DIZ_INFO/Web_Info/Application_URLs/Application_Info_URL")
strApplicationInfoURL = xmlNode.InnerText
Catch ex As Exception
strApplicationInfoURL = ""
End Try

' Required Program version, if not present, exception fired and returns false (no new version information available)
xmlNode = xmlPad.SelectSingleNode("/XML_DIZ_INFO/Program_Info/Program_Version")
' Compare Version length based on dest this is in case build information is not
' included in the version in the pad file.
strListedVersion = xmlNode.InnerText.Trim()
If strCurrentVersion.StartsWith(xmlNode.InnerText.Trim()) Then
CheckVersion = False
Else
CheckVersion = True
End If
xmlPad = Nothing
System.IO.File.Delete(strXMLFileName)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
CheckVersion = False
End Try
End Function
Public Function GetURL(ByVal destURL As String) As String
Dim oWebReq As HttpWebRequest = CType(WebRequest.Create(destURL), HttpWebRequest)
Dim oWebResp As HttpWebResponse = CType(oWebReq.GetResponse(), HttpWebResponse)
Dim oStream As Stream = oWebResp.GetResponseStream()
Dim objXMLStream As StreamWriter
GetURL = System.IO.Path.GetTempFileName
Debug.WriteLine(GetURL)
objXMLStream = System.IO.File.CreateText(GetURL)
Dim iByte As Integer
iByte = oStream.ReadByte
While iByte <> -1
objXMLStream.Write(Chr(iByte))
iByte = oStream.ReadByte
End While
objXMLStream.Close()
End Function
End Class

1 comment:

raimund said...

hi rob,
this example was very helpful in showing the use or streams in xmldocs.
thanks
raimund