// Helper function to read the response from a Url and return a version state machine internal static async Task <VersionState> GetVersionFromSource(string pageSource) { // Create an instance of an HtmlAgilityPack Html document that we can load the Url response into HtmlDocument updateDoc = new HtmlDocument(); // Create a default state machine of there being no new update VersionState state = new VersionState() { IsNewVersion = false, Url = String.Empty, Version = string.Empty }; //Load the page source into the HtmlAgilityPack document so that we can parse the DOM effectively updateDoc.LoadHtml(pageSource); // Find any 'H3' elements in the document var pageH3s = updateDoc.DocumentNode.Descendants("h3"); if (pageH3s != null) { // The first one that has a 'Zip' file as its source, is our upgrade file var firstZipLink = pageH3s.Select(h3 => h3.Descendants("a").FirstOrDefault(a => a.Attributes["href"] != null && a.Attributes["href"].Value != null && a.Attributes["href"].Value.ToLower().Contains(".zip"))).FirstOrDefault(item => item != null); if (firstZipLink != null) { var linkVersion = firstZipLink.Attributes["data-version"]; //List<string> linkText = firstZipLink.InnerText.Split(' ').ToList(); if (linkVersion?.Value != null) { // Use the link text to get the version number string webVersionInfo = linkVersion.Value; // Get the current version number (note the process DOESN'T take into account revisions...) string appVersionInfo = ApplicationUtility.BuildVersion(); // Create an appropriate state machine for this versions ApplicationUtility.VersionState currentState = ApplicationUtility.CompareVersions(appVersionInfo, webVersionInfo); // Alter the state machine based on whether the remote version is newer if (currentState == ApplicationUtility.VersionState.WebIsNewer) { state.IsNewVersion = true; state.Version = webVersionInfo; state.Url = firstZipLink.Attributes["href"].Value; } } } } // Clear up and close the HtmlAgilityPack document updateDoc = null; return(state); }
// Helper function for comparing the currently running application version with the lastest version number // retrieved, say from a website public static ApplicationUtility.VersionState CompareVersions(string applicationVersion, string websiteVersion) { // Default to the version numbers being the same ApplicationUtility.VersionState versionState = ApplicationUtility.VersionState.Same; // Strip out the version number 'dots' to create a single integer number and compare the two int num = new Version(applicationVersion.Replace(",", ".")).CompareTo(new Version(websiteVersion.Replace(",", "."))); if (num < 0) { versionState = ApplicationUtility.VersionState.WebIsNewer; } else if (num > 0) { versionState = ApplicationUtility.VersionState.WebIsOlder; } return(versionState); }