using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Text; using System.Windows.Forms; using System.Xml; using Microsoft.Win32; using System.Diagnostics; namespace sharpupdater { public class SharpUpdater { public static string DefaultLatestVersionMessage = "Congratulations, you are running the latest version."; public static string DefaultUpdateMessage = "You are running an outdated version. Please update to latest version."; public static string DefaultUpdateQuestion = "You are running an outdated version. Would you like to udpate?"; public string Url { get; set; } public Update UpdateInfo { get; set; } public SharpUpdater(string url) { this.Url = url; } private string GetLatestVersionFile() { string ret = string.Empty; try { WebRequest request = HttpWebRequest.Create(Url); request.Proxy = WebRequest.DefaultWebProxy; request.Proxy.Credentials = CredentialCache.DefaultCredentials; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { Stream stream = response.GetResponseStream(); TextReader reader = new StreamReader(stream); ret = reader.ReadToEnd(); reader.Close(); } response.Close(); } catch (WebException ex) { throw new UnableToRetrieveVersionException(ex.Message); } return ret; } private List GetProductInfos() { List products = new List(); XmlDocument document = new XmlDocument(); string versionFile = GetLatestVersionFile(); if (string.IsNullOrEmpty(versionFile)) { return null; } document.LoadXml(versionFile); foreach (XmlNode productsNode in document.ChildNodes) { if (productsNode.Name == "products") { foreach (XmlNode productNode in productsNode.ChildNodes) { Product product = new Product( productNode.Attributes["name"].Value, productNode.Attributes["version"].Value); products.Add(product); foreach (XmlNode node in productNode.ChildNodes) { switch (node.Name) { case "update": Update.UpdateType updateType = Update.UpdateType.Url; switch (node.Attributes["type"].Value) { case "setup": updateType = Update.UpdateType.Setup; break; case "package": updateType = Update.UpdateType.Package; break; } Update update = new Update(updateType, node.InnerText); product.Update = update; break; case "info": Info.InfoType infoType = Info.InfoType.Url; switch (node.Attributes["type"].Value) { case "file": infoType = Info.InfoType.File; break; case "notes": infoType = Info.InfoType.Notes; break; } Info info = new Info(infoType, node.InnerText); product.Info = info; break; } } } } } return products; } public Product GetProductInfo(string productName) { List products = GetProductInfos(); if (products == null) { return null; } foreach (Product product in products) { if (product.Name == productName) { return product; } } return null; } public bool IsLatestVersion(string productName, string version) { bool ret = false; Product product = GetProductInfo(productName); if (product != null) { ret = product.Version.CompareTo(version) <= 0; } return ret; } public void UpdateProduct(string productName, string version) { if (!IsLatestVersion(productName, version)) { Product product = GetProductInfo(productName); if (product != null) { bool updateRequired = false; if (product.Update != null) { updateRequired = MessageBox.Show(DefaultUpdateQuestion, "Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes; if (updateRequired) { switch (product.Update.Type) { case Update.UpdateType.Url: Process p = new Process(); p.StartInfo.FileName = GetDefaultBrowserPath(); p.StartInfo.Arguments = product.Update.Url; p.Start(); break; case Update.UpdateType.Setup: break; case Update.UpdateType.Package: break; } } } // No update strategy has been specified else { MessageBox.Show(DefaultUpdateMessage, "Update", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } private string GetDefaultBrowserPath() { string key = @"htmlfile\shell\open\command"; RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(key, false); return ((string)registryKey.GetValue(null, null)).Split('"')[1]; } } }