Книга: C# 2008 Programmer

Programmatically Updating the Application

Programmatically Updating the Application

Instead of the application checking for updates before it starts, it would be a good idea for users to be able to choose when they want to check for updates. For that, add a new button to the form, as shown in Figure 16-27.


Figure 16-27

Import the following namespace:

using System.Deployment.Application;

Code the Update button like this:

private void btnUpdate_Click(object sender, EventArgs e) {
 //---check if the application is deployed by ClickOnce---
 if (ApplicationDeployment.IsNetworkDeployed) {
  //---Get an instance of the deployment---
  ApplicationDeployment deployment =
   ApplicationDeployment.CurrentDeployment;
  //---if there is any update---
  if (deployment.CheckForUpdate()) {
   DialogResult response =
    MessageBox.Show(("A new version of the " +
     "application is available. " +
     "Do you want to update application?"),
     ("Application Updates"), MessageBoxButtons.YesNo);
   //---if user wants to update---
   if (response == DialogResult.Yes) {
    Cursor.Current = Cursors.WaitCursor;
    //---update the application---
    deployment.Update();
    //---prompt the user to restart---
    MessageBox.Show("Update completed. You need to restart" +
     " the application.", ("Update Completed"),
     MessageBoxButtons.OK, MessageBoxIcon.Information);
    //---restart the application---
    Application.Restart();
   }
  } else {
   //---application is up-to-date---
   MessageBox.Show(("Application is up-to-date."), "Update",
    MessageBoxButtons.OK, MessageBoxIcon.Information);
  }
 } else {
  //---application is not installed using ClickOnce---
  MessageBox.Show(("Application is not installed " +
   "using ClickOnce"), ("Updates not available"),
   MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
}

You first check to see if the application is deployed using ClickOnce. This can be done by using the IsNetworkDeployed property from the ApplicationDeployment static class. If the application is indeed deployed using ClickOnce, you proceed to obtain an instance of the deployment using the currentDeployment property of the ApplicationDeployment class. Using this instance of the deployment, you call the CheckForUpdate() method to check whether there is a newer version of the application available from the publishing server. If there is, you prompt the user by asking if he wants to update the application. If he does, you update the application, using the Update() method. After that, you force the user to restart the application, using the Restart() method.

To test the update, first run an instance of the PhotoViewer application by launching it from the Start menu. Next, republish the application in Visual Studio 2008. Click the Update button to see if an update is available. You should see the prompt shown in Figure 16-28. Click Yes, and the application will be updated.


Figure 16-28

Оглавление книги


Генерация: 0.038. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз