C# – Self updating application …

So, onto the method source …

Update Method:

private void frmMain_Load(object sender, EventArgs e)
{
  string pathToExecutable = Application.StartupPath.ToString();
  string upgradeDomain = "http://www.digitalformula.net";
  string upgradeDirectory = "countdown";
  string downloadExecutable = "Countdown.exe";
  string upgradeExecutable = "Countdown.upgrade.exe";
  string upgradeUrl = String.Format("{0}/{1}/{2}", upgradeDomain, upgradeDirectory, downloadExecutable);
  string upgradeFolderLocal = pathToExecutable;
  string upgradeFileLocal = String.Format("{0}\{1}", upgradeFolderLocal, upgradeExecutable);
  string oldExecutable = String.Format("{0}\{1}", pathToExecutable, downloadExecutable);
  try
  {
    lblWaitingDone.Text = "done!";
    lblDownloadingDone.Text = "working";
    System.Net.WebClient client = new System.Net.WebClient();
    client.DownloadFile(upgradeUrl, upgradeFileLocal);
    lblDownloadingDone.Text = "done!";
    lblRenamingDone.Text = "working";
    File.Delete(oldExecutable);
    File.Move(upgradeFileLocal, oldExecutable);
    lblRenamingDone.Text = "done!";
    ProcessStartInfo upgradeProcess = new ProcessStartInfo(oldExecutable);
    upgradeProcess.WorkingDirectory = pathToExecutable;
    Process.Start(upgradeProcess);
    Environment.Exit(0);
    lblEnablingDone.Text = "done!";
  }
  catch (Exception ex)
  {
    DialogResult result = MessageBox.Show(String.Format("Error during upgrade.n{0}", ex.Message.ToString()));
  }
}

Have a go – I’m very interested to hear if anyone tries this technique to update their own application. 🙂