C# – Self updating application …

The application then checks to see if the downloaded version file exists in the TEMP directory (defined by the environment variable %TEMP%) and then reads the version information from it. The first part of that code looks like this.

Check for new download:

if (File.Exists(config.VersionFileLocal))
{
  string versionFileLocal = String.Format("{0}\{1}", System.Environment.ExpandEnvironmentVariables("%TEMP%"), "countdown.version.txt");
  TextReader tr = new StreamReader(versionFileLocal);
  string tempStr = tr.ReadLine();
  tr.Close();
  File.Delete(versionFileLocal);
  string longVersionFromFile = tempStr;
  string shortVersionFromFile = tempStr.Replace(".", String.Empty);
  Version vrs = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
  string longVersionFromVrs = String.Format("{0}.{1}.{2}.{3}", vrs.Major, vrs.Minor, vrs.MajorRevision, vrs.MinorRevision);
  string shortVersionFromVrs = String.Format("{0}{1}{2}{3}", vrs.Major, vrs.Minor, vrs.MajorRevision,   vrs.MinorRevision);
...
}
else
{
  DialogResult result = MessageBox.Show(String.Format("Version file not found or not accessible.nPlease check that you have permission to read the {0} folder", versionFolderLocal), "Version information missing", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

That code opens the version text file, reads the first line from it, closes the file (important!) then gets a ‘fake’ number by removing all the version separator symbols from the version it has read.

Version comparison

Once the data reading process is complete the version read from the file and version of the currently running application are compared using the following code (I’ll get to the … bits in a minute). This code snippet also runs the actual upgrade process by calling AppStart.exe if the user says Yes when asked if they want to upgrade (assuming an updated version is available).

Compare current and new versions:

if (Convert.ToInt32(shortVersionFromVrs) < Convert.ToInt32(shortVersionFromFile))
{
  DialogResult result = MessageBox.Show(String.Format("Countdown upgrade from version {0} to {1} is now available!  Upgrade now?", longVersionFromVrs, longVersionFromFile), "Upgrade available", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  switch (result)
  {
    case DialogResult.Yes:
    {
      string pathToExecutable = Application.StartupPath.ToString();
      string upgradeExecutable = "AppStart.exe";
      string fullUpgradeExecutable = String.Format("{0}\{1}", pathToExecutable, upgradeExecutable);
      if (File.Exists(fullUpgradeExecutable))
      {
        ProcessStartInfo upgradeProcess = new ProcessStartInfo(fullUpgradeExecutable);
        upgradeProcess.WorkingDirectory = pathToExecutable;
        Process.Start(upgradeProcess);
        Environment.Exit(0);
      }
      else
      {
        DialogResult result2 = MessageBox.Show(String.Format("{0} not found in {1}.nPlease check that this file exists then try the upgrade process again.", upgradeExecutable, pathToExecutable), "Upgrade Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
      break;
    }
    default:
    {
      break;
    }
  }
}
else
{
  DialogResult result = MessageBox.Show(String.Format("There are no upgrades currently available.  You have the latest version of Countdown."), "No upgrade available", MessageBoxButtons.OK, MessageBoxIcon.Information);
}