Bổ sung tính năng tự động cập nhập vào ứng dụng C#

Adding “Check for update” feature to your C# application

Today I decided I would add a ‘check for update’ option to My C# application. This is quite useful feature, especially when you host your application on many hosting servers (like download.com and others). In this case it can be difficult for the user to check if there is a newer version of your software available, because:

    *  the user does not remember where he downloaded the application from
   * there is an older version on the hosting server, so the user is not aware that there is a newer version of your app available   
    * simple, but very common reason: the user is to lazy to look for the new version; because it’s easier to simply select the ‘update’ option in your application, it may work for lazy usersBeside those reasons this is a nice, small feature we can practice our c# programming skills on :). At least for me, because I’ve spent last two years mainly developing server modules in Python.
Two major subjects are:   
     * HTTP file downloading   
     * Simple XML parsing (XmlTextReader)
Let’s start.

General idea

First we need a way to provide our application with the information about the newest version. So what we do is put a little xml file anywhere on Internet (well, preferably on your homepage). In this xml file we store the number of the newest version of our app. While checking for update we download this xml, parse it and compare the version of running application with the version from the xml file.

Downloading the xml file

This is very easy - we just provide the XmlTextReader with the URL of our XML file. The XmlTextReader will download the file for us. What’s even better, it uses the .NET 2.0 internal solutions that detect the IE proxy settings (I’ve double-checked this and it really works :)), so this will work even when the user connects through a proxy.

Parsing the xml

As mentioned above, we will use XmlTextReader (just remember to use System.Xml). First take a look at our litte xml contaning the information about the newest version:

view plaincopy to clipboardprint?
  1. < ?xml version="1.0" encoding="utf-8"?>  
  2. <ourfancyapp>  
  3.   <version>1.2.3.4444</version>  
  4.   <url>http://some.domain/our_app_homepage/</url>  
  5. </ourfancyapp>  

As you probably noticed we store the version information in the same string format as c# Application.ProductVersion. I’ve chosen this format because it will be easier to parse and compare the version numbers, as you will see later.

Ok, let use XmlTextReader to parse the xml:

view plaincopy to clipboardprint?
  1. // in newVersion variable we will store the  
  2. // version info from xml file  
  3. Version newVersion = null;  
  4. // and in this variable we will put the url we  
  5. // would like to open so that the user can  
  6. // download the new version  
  7. // it can be a homepage or a direct  
  8. // link to zip/exe file  
  9. string url = "";  
  10. try  
  11. {  
  12.  // provide the XmlTextReader with the URL of  
  13.  // our xml document  
  14.  string xmlURL = "http://domain/app_version.xml";  
  15.  XmlTextReader reader =  
  16.    new XmlTextReader(xmlURL);  
  17.  // simply (and easily) skip the junk at the beginning  
  18.  reader.MoveToContent();  
  19.  // internal - as the XmlTextReader moves only  
  20.  // forward, we save current xml element name  
  21.  // in elementName variable. When we parse a  
  22.  // text node, we refer to elementName to check  
  23.  // what was the node name  
  24.  string elementName = "";  
  25.  // we check if the xml starts with a proper  
  26.  // "ourfancyapp" element node  
  27.  if ((reader.NodeType == XmlNodeType.Element) &&  
  28.      (reader.Name == "ourfancyapp"))  
  29.  {  
  30.   while (reader.Read())  
  31.   {  
  32.    // when we find an element node,  
  33.    // we remember its name  
  34.    if (reader.NodeType == XmlNodeType.Element)  
  35.     elementName = reader.Name;  
  36.    else  
  37.    {  
  38.     // for text nodes...  
  39.     if ((reader.NodeType == XmlNodeType.Text) &&  
  40.         (reader.HasValue))  
  41.     {  
  42.      // we check what the name of the node was  
  43.      switch (elementName)  
  44.      {  
  45.      case "version":  
  46.       // thats why we keep the version info  
  47.       // in xxx.xxx.xxx.xxx format  
  48.       // the Version class does the  
  49.       // parsing for us  
  50.       newVer = new Version(reader.Value);  
  51.       break;  
  52.      case "url":  
  53.       url = reader.Value;  
  54.       break;  
  55.      }  
  56.     }  
  57.    }  
  58.   }  
  59.  }  
  60.  reader.Close();  
  61. }  
  62. catch (Exception)  
  63. {  
  64. }  

That’s how we parse our XML. Now we check if the newVersion and url are not empty. If they’re not, it means we successfuly downloaded and parsed the xml file and now we can…

…compare the versions

The Application.ProductVersion is just a string, and it’s usually not sensible to compare strings containig numbers. Luckily in the .NET executing assembly there is a variable, which happens to be the same type as our newVersion. It makes the comparing a lot easier :) :

view plaincopy to clipboardprint?
  1. // get the running version  
  2. Version curVersion =  
  3.  System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;  
  4. // compare the versions  
  5. if (curVersion.CompareTo(newVer) < 0)  
  6. {  
  7.  // ask the user if he would like  
  8.  // to download the new version  
  9.  string title = "New version detected.";  
  10.  string question = "Download the new version?";  
  11.  if (DialogResult.Yes ==  
  12.   MessageBox.Show(this, question, title,  
  13.                   MessageBoxButtons.YesNo,  
  14.                   MessageBoxIcon.Question))  
  15.   {  
  16.    // navigate the default web  
  17.    // browser to our app  
  18.    // homepage (the url  
  19.    // comes from the xml content)  
  20.    System.Diagnostics.Process.Start(url);  
  21.   }  
  22. }  

If the user answers ‘Yes’, we navigate the default browser to the URL we have from the downloaded xml file. I didn’t hardcode this URL in the code, because when we for example move our homepage, we would be in trouble. And in our solution we can simply change the url in the xml file and the users would be automatically directed to the new homepage.

Well, that’s all. As you see this was a pretty simple task. Hope you found it useful.

Enjoy,

Bùi Ngọc Anh 

Tham khảo: http://themech.net/2008/05/adding-check-for-update-option-in-csharp/ 

 


 


              Tư vấn 1                               Tư vấn 2

 

download game. casino online. latest movies. free movie downloads