C# – Self updating application …

Share on facebook
Share on twitter
Share on linkedin

If the name resolution fails the following code is run in the catch block above (complete catch block shown below). It simply shows an error message with an OK button.

IP Address resolution failed:

catch
{
  DialogResult result = MessageBox.Show(String.Format("Unable to connect to {0}.nPlease check your internet connection and try again.", RemoteDomain), "Error during version check", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

If the name was resolved correctly it then tries to download the version update file into the current user’s TEMP directory by running the following try block.

Download new version:

try
{
  string versionFileUrl = "http://www.digitalformula.net/countdown/countdown.version.txt";
  System.Net.WebClient client = new System.Net.WebClient();
  client.DownloadFile(versionFileUrl, System.Environment.ExpandEnvironmentVariables("%TEMP%"));
}
catch (Exception ex)
{
...
}

Note that the version information simply contains the text “2.0.0.1” (without the quotes). When writing this article the application version was 2.0.0.0
To set the version of the application you need to open the AssemblyInfo.cs file – you can find this file under the project name and then by clicking Properties (assuming your project is C# like this example). Change [assembly: AssemblyVersion(“2.0.0.0”)] to the appropriate version.

Download failure

If the download fails the following complete catch block is executed.

Download failed:

catch (Exception ex)
{
  string errorDetails = String.Empty;
  MessageBoxIcon iconsToShow = MessageBoxIcon.Information;
  if (ex.Message.Contains("could not be resolved"))
  {
    errorDetails = String.Format("Error looking up {0}.nPlease check your internet connection and try again.", RemoteDomain);
    iconsToShow = MessageBoxIcon.Error;
  }
  else if (ex.Message.Contains("404"))
  {
    errorDetails = "Upgrades to Countdown are currently unavailable.nPlease try again later.";
    iconsToShow = MessageBoxIcon.Information;
  }
  DialogResult result = MessageBox.Show(String.Format("{0}", errorDetails), "Error downloading file", MessageBoxButtons.OK, iconsToShow);
  return;
}