Advertisement
ra1n

Check for Updates

Dec 20th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. using System.Windows.Forms;
  8. using System.Diagnostics;
  9.  
  10. namespace GME
  11. {
  12.     class CheckUpdate
  13.     {
  14.         public static void Updater()
  15.         {
  16.             string downloadUrl = "";
  17.             Version newVersion = null;
  18.             string aboutUpdate = "";
  19.             string xmlUrl = "http://www.example.com/update.xml";
  20.             XmlTextReader reader = null;
  21.             try
  22.             {
  23.                 reader = new XmlTextReader(xmlUrl);
  24.                 reader.MoveToContent();
  25.                 string elementName = "";
  26.                 if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "appinfo"))
  27.                 {
  28.                     while (reader.Read())
  29.                     {
  30.                         if (reader.NodeType == XmlNodeType.Element)
  31.                         {
  32.                             elementName = reader.Name;
  33.                         }
  34.                         else
  35.                         {
  36.                             if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue))
  37.                                 switch (elementName)
  38.                                 {
  39.                                     case "version":
  40.                                         newVersion = new Version(reader.Value);
  41.                                         break;
  42.                                     case "url":
  43.                                         downloadUrl = reader.Value;
  44.                                         break;
  45.                                     case "about":
  46.                                         aboutUpdate = reader.Value;
  47.                                         break;
  48.                                 }
  49.                         }
  50.                     }
  51.                 }
  52.             }
  53.             catch (Exception ex)
  54.             {
  55.                 MessageBox.Show(ex.Message);
  56.                 Environment.Exit(1);
  57.             }
  58.             finally
  59.             {
  60.                 if (reader != null)
  61.                     reader.Close();
  62.             }
  63.             Version applicationVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
  64.             if (applicationVersion.CompareTo(newVersion) < 0)
  65.             {
  66.                 string str = String.Format("New Update Found\n\nNewest version : {1} \nLog : {2} ", applicationVersion, newVersion, aboutUpdate);
  67.                 if (DialogResult.No != MessageBox.Show(str + "\n\nWould you like to download this update?", "Check for updates", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
  68.                 {
  69.                     try
  70.                     {
  71.                         Process.Start(downloadUrl);
  72.                     }
  73.                     catch { }
  74.                     return;
  75.                 }
  76.                 else
  77.                 {
  78.                     ;
  79.                 }
  80.             }
  81.             else
  82.             {
  83.                 MessageBox.Show("No new updates found.", "Check for Updates", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement