Jun 292011
 

Are the users of the application that you developed using the latest version? If your application doesn’t have automatic checking built in, you have to rely on the users to periodically check the site where you host the software for updates, or else you have to email the users or otherwise notify them. Certainly, the very best solution is to have your application self check itself to see if a newer version is available. I will present a simple solution here that uses a small php script placed on a web server. Data about my applications is stored in a mysql database. Then I will show you how I have a C# application check for updates. It’s a really easy solution and anyone with a little programming experience will be able to do it. First, here is the php code that is placed on the server where I host my applications for download.

<!--?php
/*
* To use, place this on a web server of your choice.  Next, create a mysql database
* where you will store information about your applications, such as the latest version.
* From now on, when you update an application, you will also need to update the
* information in the database about that application. Configure the database with the
* following columns: app_name, latest_version, build_date, url
*
* Then, in your application, when it checks for updates, have it send a request to the
* web server formed like this:
* http://myserver.com/app_updates.php?app_name=MyAppName
* where MyAppName above is exactly the same as the app_name stored in the mysql
* database. If MyAppName is found in the database, then it will return a small
* xml string with the data for that record.  If MyAppName is not found, it will
* return nothing. The xml output will look like this
*
* <?xml version="1.0"?-->
*
*   SevereWeatherGrowler
*   1.2.0
*   20110629
*   http://skipstechtalk.net/severe-weather-growler/
*
*
*
* It is up to your application to decide what to do with the returned data. For
* instance, one way to check if an update is available is to always store the build
* date in your application. Then when your application checks the sever, it can
* compare its build data to the build date for the latest version. If they are
* different, then an update is required. You can then direct the user to the url
* provided to get the update.
*/
$dbuser = 'my_database_user_name';
$dbname = 'my_database_name';
$dbpasswd = 'my_super_secret_password';
$dbhost = 'my_db_host'; // might be 'localhost'
$table_name = 'app_updates';
 
// test the connection
$dbh = @mysql_connect($dbhost, $dbuser, $dbpasswd);
if (!$dbh)
{
  echo "Could not connect to MySQL server on " . $dbhost;
  die();
}
 
// get the app_name from the url
$app_name = $_REQUEST['app_name'];
 
// get the data from the database for that app_name
$query = "SELECT * FROM $dbname.$table_name WHERE app_name = '$app_name'";
$result = query($query);
$row = mysql_fetch_object($result);
//print_r ($row);
 
// build the xml output
if ($row != null)
{
  $xmlstr = "<!--?xml version='1.0' ?-->\n";
  $xml = new SimpleXMLElement($xmlstr);
  $xml->addChild('app_name', $row->app_name);
  $xml->addChild('latest_version', $row->latest_version);
  $xml->addChild('build_date', $row->build_date);
  $xml->addChild('url', $row->url);
  //print_r($xml);
  echo $xml->asXml();
}
 
function query($query) {
  if(!($result = mysql_query($query)))
  {
    //can't execute query
    echo ( "Couldn't query table!\n");
    echo ( "MySQL Reports: " . mysql_error() . "\n" . "Query was: " . $query);
    exit();
  }
  return $result;
}
 
?&gt;

 

 

The code is pretty well documented (in my opinion). As it says in the comments, just have your application check http://yourwebserver.com/app_updates.php?app_name=MyAppName. The server will then return a small xml string that can be parsed by your application.  Here’s how I do it with C#

 

public const string UPDATE_CHECKER_URL = "http://my_webserver.com/app_updates.php";
public const int BUILD_DATE = 20110629;
 
public static void appUpdateTimer_Elapsed(object sender, ElapsedEventArgs e)
{
  try
  {
    WebClient wc = new WebClient();
    string updateCheckerUrl = UPDATE_CHECKER_URL + "?app_name=" +
         System.Windows.Forms.Application.ProductName;
    string updateText = wc.DownloadString(updateCheckerUrl);
    XmlDocument myXmlDoc = new XmlDocument();
    myXmlDoc.LoadXml(updateText);
 
    string latestVersion =
       myXmlDoc.SelectSingleNode("/appdata/latest_version").InnerText;
    string build_date =
       myXmlDoc.SelectSingleNode("/appdata/build_date").InnerText;
    string updateUrl =
       myXmlDoc.SelectSingleNode("/appdata/url").InnerText;
    int result = 0;
    Int32.TryParse(build_date, out result);
 
    if (BUILD_DATE != result)
    {
      MessageBox.Show("There is an update available. Please check " +
           updateUrl + " for details.");
    }
  }
  catch
  {
    //do nothing.
  }
}

Then all that’s left is to set a timer to periodically call this function.  Put this in your Main() or someplace appropriate.

appUpdateTimer.Interval = 4 * 60 * 60 * 1000; //check every four hours
appUpdateTimer.AutoReset = true;
appUpdateTimer.Elapsed += new ElapsedEventHandler(appUpdateTimer_Elapsed);
appUpdateTimer.Start();

 

 Posted by at 1:40 pm

  One Response to “Check the latest version”

  1. Hello. You seem to have keepass and the logitech harmony.

    I have a problem with inputting directly into the harmony software, which is weird because it’s the only thing not working from 312 keys.

    I tried adding * to the logitech name *logitech* and it doesn’t seem to work.

    any tips ?

    P.S : sorry, this is the only way I can contact you

Leave a Reply