daniel.porter
daniel.porter

Reputation: 101

Auto Update Function in Vb.net

Ok I got this code from Gemster from http://tech.reboot.pro/showthread.php?tid=2982. So I just looked for something to plug into my system as it need to be done asap. So I copied and pasted. However, I do understand what is going on. The only problem is that it skips over a section of important code and I can't figure out why. So here it is.

 Dim CurrentVersion As String = My.Application.Info.Version.ToString '--- Change this to Current version, needs changing on every update
    Dim ProgramName As String = My.Application.Info.AssemblyName '--- Change this to Your Program Name
    Dim SiteName As String = "http://somewebsite.com/UpdateVersion.html" '--- Change this to Your Update page
    Dim VersionCHK As String = ""
    Dim GetVer As String = ""
    Dim GetVerLink As String = ""
    Dim GetUpd As Integer

    'Web Request
    Dim WebRequest As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(SiteName)
    Dim WebResponse As System.Net.HttpWebResponse = WebRequest.GetResponse
    Dim STR As System.IO.StreamReader = New System.IO.StreamReader(WebResponse.GetResponseStream())
    Dim ReadSource As String = Str.ReadToEnd
    Dim Regex As New System.Text.RegularExpressions.Regex(ProgramName & "=(\d+).(\d+)=(.*?).zip")
    Dim matches As MatchCollection = Regex.Matches(ReadSource)

This is the part that it skips the 'Split String' for each statement

    'Split String
    For Each match As Match In matches
        Dim RegSplit() As String = Split(Match.ToString, "=")
        GetVer = RegSplit(1)
        GetVerLink = RegSplit(2)
    Next

This is where it picks back up.

    'Check Verison
    If GetVer > CurrentVersion Then
        GetUpd = MsgBox(ProgramName & " is an old version." & vbCrLf & "New Update is available" & _
        vbCrLf & "Current version: " & CurrentVersion & vbCrLf & "Version Avalible: " & _
        GetVer & vbCrLf & vbCrLf & "Update Now?", vbYesNo, "Update")

        If GetUpd = vbYes Then
            Dim sfd As New SaveFileDialog
            sfd.FileName = IO.Path.GetFileName(GetVerLink)
            If sfd.ShowDialog = DialogResult.OK Then
                My.Computer.Network.DownloadFile(GetVerLink, sfd.FileName)
            End If
        End If
    Else
        MsgBox(ProgramName & " is upto date." & vbCrLf & "Current version: " & CurrentVersion, 0, "Update")
    End If
    Return vbNull

Any ideas why? Thanks in advance.

Upvotes: 0

Views: 2983

Answers (1)

Steve
Steve

Reputation: 20469

More than likely the regex is incorrect thus matches is empty.

I would suggest using an xml file rather than trying to parse html with regex:

The XML on your server:

            <VersionHistory>
                <CurrentVersion>
                    <VersionNumber>
                        1.0.0.2
                    </VersionNumber>
                    <DownloadLink>
                        http://www.mysite.com/downloads/myapp_1.0.0.2.zip
                    </DownloadLink>
                </CurrentVersion>
                <LegacyVersions>
                    <VersionNumber>
                        1.0.0.1
                    </VersionNumber>
                    <DownloadLink>
                        http://www.mysite.com/downloads/myapp_1.0.0.1.zip
                    </DownloadLink>
                </LegacyVersions>
            </VersionHistory>

The code:

    Dim localVersion As String = My.Application.Info.Version.ToString
    Dim wc As New Net.WebClient
    wc.Proxy = Nothing

    Dim source As String = String.Empty

    Try
        source = wc.DownloadString("http://www.mysite.com/versions.xml")
    Catch ex As Net.WebException
        'handle accordingly
    End Try

    Dim xm As New Xml.XmlDocument
    xm.LoadXml(source)

    Dim currentVersion As String = xm.SelectSingleNode("//CurrentVersion").ChildNodes(0).InnerText.Trim
    Dim currentLink As String = xm.SelectSingleNode("//CurrentVersion").ChildNodes(1).InnerText.Trim

    If localVersion = currentVersion Then
        MessageBox.Show("Up to Date")
    Else
        'run update routine, use currentLink to download latest version
    End If

Upvotes: 1

Related Questions