/// <summary> /// Parse the returned xml from the website request to extract version information /// </summary> /// <param name="result">version xml information</param> /// <returns>new WinAuthVersionInfo object</returns> private WinAuthVersionInfo ParseGetLatestVersion(string result) { // load xml document and pull out nodes XmlDocument xml = new XmlDocument(); xml.LoadXml(result); var node = xml.SelectSingleNode("//version"); Version version = null; #if NETFX_4 Version.TryParse(node.InnerText, out version); #endif #if NETFX_3 try { version = new Version(FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion); } catch (Exception) { } #endif if (node != null && version != null) { WinAuthVersionInfo latestversion = new WinAuthVersionInfo(version); DateTime released; node = xml.SelectSingleNode("//released"); if (node != null && DateTime.TryParse(node.InnerText, out released) == true) { latestversion.Released = released; } #if NETFX_4 node = xml.SelectSingleNode("//url"); #endif #if NETFX_3 node = xml.SelectSingleNode("//url35"); #endif if (node != null && string.IsNullOrEmpty(node.InnerText) == false) { latestversion.Url = node.InnerText; } node = xml.SelectSingleNode("//changes"); if (node != null && string.IsNullOrEmpty(node.InnerText) == false) { latestversion.Changes = node.InnerText; } return(latestversion); } else { throw new InvalidOperationException("Invalid return data"); } }
/// <summary> /// Explicitly get the latest version information. Will be asynchronous if a callback is provided. /// </summary> /// <param name="callback">optional callback for async operation</param> /// <returns>latest WinAuthVersionInfo or null if async</returns> public virtual WinAuthVersionInfo GetLatestVersion(Action <WinAuthVersionInfo, bool, Exception> callback = null) { // get the update URL from the config else use the default string updateUrl = WinAuthMain.WINAUTH_UPDATE_URL; try { var settings = new System.Configuration.AppSettingsReader(); string appvalue = settings.GetValue("UpdateCheckUrl", typeof(string)) as string; if (string.IsNullOrEmpty(appvalue) == false) { updateUrl = appvalue; } } catch (Exception) { } try { using (WebClient web = new WebClient()) { web.Headers.Add("User-Agent", "WinAuth-" + this.CurrentVersion.ToString()); if (callback == null) { // immediate request string result = web.DownloadString(updateUrl); WinAuthVersionInfo latestVersion = ParseGetLatestVersion(result); if (latestVersion != null) { // update local values LastKnownLatestVersion = latestVersion.Version; Config.WriteSetting(WINAUTHREGKEY_LATESTVERSION, latestVersion.Version.ToString(3)); } return(latestVersion); } else { // initiate async operation web.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GetLatestVersionDownloadCompleted); web.DownloadStringAsync(new Uri(updateUrl), callback); return(null); } } } catch (Exception) { // don't fail if we can't get latest version return(null); } }
/// <summary> /// Callback from Updater object with latest version information /// </summary> /// <param name="latestInfo">latest version or null</param> /// <param name="cancelled">flag if operation was cancelled</param> /// <param name="error">any error exception</param> private void Updater_GetLatestVersionCompleted(WinAuthVersionInfo latestInfo, bool cancelled, Exception error) { if (IsDisposed || IsHandleCreated == false) { return; } var text = string.Empty; if (cancelled) { text = "Update was cancelled"; } else if (error != null) { text = GetHtmlText("Error: " + error.Message); //text = "Error: " + error.Message; } else { var latest = new Version(latestInfo.Version.Major, latestInfo.Version.Minor, latestInfo.Version.Build); var current = new Version(Updater.CurrentVersion.Major, Updater.CurrentVersion.Minor, Updater.CurrentVersion.Build); if (current >= latest) { text = GetHtmlText("<p>Latest Version: {0}</p><p>You are on the latest version.</p>", latest.ToString(3)); } else { var info = string.Format( "<p>Latest Version: {0} (yours {1})</p><p><a href=\"{2}\">Download version {0}</a></p></body></html>", latest.ToString(3), current.ToString(3), latestInfo.Url); text = GetHtmlText("{0}", info); } } // update the textbox in a delegate Invoke((MethodInvoker) delegate { versionInfoLabel.Text = text; }); }
/// <summary> /// Parse the returned xml from the website request to extract version information /// </summary> /// <param name="result">version xml information</param> /// <returns>new WinAuthVersionInfo object</returns> private WinAuthVersionInfo ParseGetLatestVersion(string result) { // load xml document and pull out nodes XmlDocument xml = new XmlDocument(); xml.LoadXml(result); var node = xml.SelectSingleNode("//version"); Version version; if (node != null && Version.TryParse(node.InnerText, out version) == true) { WinAuthVersionInfo latestversion = new WinAuthVersionInfo(version); DateTime released; node = xml.SelectSingleNode("//released"); if (node != null && DateTime.TryParse(node.InnerText, out released) == true) { latestversion.Released = released; } node = xml.SelectSingleNode("//url"); if (node != null && string.IsNullOrEmpty(node.InnerText) == false) { latestversion.Url = node.InnerText; } node = xml.SelectSingleNode("//changes"); if (node != null && string.IsNullOrEmpty(node.InnerText) == false) { latestversion.Changes = node.InnerText; } return(latestversion); } else { throw new InvalidOperationException("Invalid return data"); } }
/// <summary> /// Callback for async operation for latest version web request /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void GetLatestVersionDownloadCompleted(object sender, DownloadStringCompletedEventArgs args) { // no point if e have no callback Action <WinAuthVersionInfo, bool, Exception> callback = args.UserState as Action <WinAuthVersionInfo, bool, Exception>; if (callback == null) { return; } // report cancelled or error if (args.Cancelled == true || args.Error != null) { callback(null, args.Cancelled, args.Error); return; } try { // extract the latest version WinAuthVersionInfo latestVersion = ParseGetLatestVersion(args.Result); if (latestVersion != null) { // update local values LastKnownLatestVersion = latestVersion.Version; Config.WriteSetting(WINAUTHREGKEY_LATESTVERSION, latestVersion.Version.ToString(3)); } // perform callback callback(latestVersion, false, null); } catch (Exception ex) { // report any other error callback(null, false, ex); } }
/// <summary> /// Parse the returned xml from the website request to extract version information /// </summary> /// <param name="result">version xml information</param> /// <returns>new WinAuthVersionInfo object</returns> private WinAuthVersionInfo ParseGetLatestVersion(string result) { // load xml document and pull out nodes XmlDocument xml = new XmlDocument(); xml.LoadXml(result); var node = xml.SelectSingleNode("//version"); Version version = null; #if NETFX_4 Version.TryParse(node.InnerText, out version); #endif #if NETFX_3 try { version = new Version(FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion); } catch (Exception) { } #endif if (node != null && version != null) { WinAuthVersionInfo latestversion = new WinAuthVersionInfo(version); DateTime released; node = xml.SelectSingleNode("//released"); if (node != null && DateTime.TryParse(node.InnerText, out released) == true) { latestversion.Released = released; } node = xml.SelectSingleNode("//url"); if (node != null && string.IsNullOrEmpty(node.InnerText) == false) { latestversion.Url = node.InnerText; } node = xml.SelectSingleNode("//changes"); if (node != null && string.IsNullOrEmpty(node.InnerText) == false) { latestversion.Changes = node.InnerText; } return latestversion; } else { throw new InvalidOperationException("Invalid return data"); } }
/// <summary> /// Parse the returned xml from the website request to extract version information /// </summary> /// <param name="result">version xml information</param> /// <returns>new WinAuthVersionInfo object</returns> private WinAuthVersionInfo ParseGetLatestVersion(string result) { // load xml document and pull out nodes XmlDocument xml = new XmlDocument(); xml.LoadXml(result); var node = xml.SelectSingleNode("//version"); Version version; if (node != null && Version.TryParse(node.InnerText, out version) == true) { WinAuthVersionInfo latestversion = new WinAuthVersionInfo(version); DateTime released; node = xml.SelectSingleNode("//released"); if (node != null && DateTime.TryParse(node.InnerText, out released) == true) { latestversion.Released = released; } node = xml.SelectSingleNode("//url"); if (node != null && string.IsNullOrEmpty(node.InnerText) == false) { latestversion.Url = node.InnerText; } node = xml.SelectSingleNode("//changes"); if (node != null && string.IsNullOrEmpty(node.InnerText) == false) { latestversion.Changes = node.InnerText; } return latestversion; } else { throw new InvalidOperationException("Invalid return data"); } }
/// <summary> /// Callback from Updater object with latest version information /// </summary> /// <param name="latestInfo">latest version or null</param> /// <param name="cancelled">flag if operation was cancelled</param> /// <param name="error">any error exception</param> void Updater_GetLatestVersionCompleted(WinAuthVersionInfo latestInfo, bool cancelled, Exception error) { if (this.IsDisposed == true || IsHandleCreated == false) { return; } string text = string.Empty; if (cancelled == true) { text = "Update was cancelled"; } else if (error != null) { text = GetHtmlText("Error: " + error.Message); //text = "Error: " + error.Message; } else { Version latest = new Version(latestInfo.Version.Major, latestInfo.Version.Minor, latestInfo.Version.Build); Version current = new Version(Updater.CurrentVersion.Major, Updater.CurrentVersion.Minor, Updater.CurrentVersion.Build); if (current >= latest) { text = GetHtmlText("<p>Latest Version: {0}</p><p>You are on the latest version.</p>", latest.ToString(3)); } else { string info = string.Format("<p>Latest Version: {0} (yours {1})</p><p><a href=\"{2}\">Download version {0}</a></p></body></html>", latest.ToString(3), current.ToString(3), latestInfo.Url); text = GetHtmlText("{0}", info); } } // update the textbox in a delegate this.Invoke((MethodInvoker)delegate { versionInfoLabel.Text = text; }); }