By default, the application checks for updates every time before it starts.
When the user closes and then relaunches the PhotoViewer application, he gets a prompt, as shown in Figure 16-25.
Figure 16-25
The user can click OK to download the updated application, or click Skip if he doesn't want to update the application now. The updated application will look like Figure 16-26.
Figure 16-26
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
Once an application is updated, the user has a choice to roll it back to its previous version. To do so, go to the Control Panel and run the Add or Remove Programs application. Locate the application (in this case, PhotoViewer) and click on the Change/Remove button. You have two choices — restore the application to its previous state or remove the application from the computer (see Figure 16-29).
Figure 16-29
An application can be rolled back only to its previous version. If it's been updated several times, it only rolls back to the version preceding the last update.
Under the Hood: Application and Deployment Manifests
When you use the Publish Wizard to publish your application using ClickOnce, Visual Studio 2008 publishes your application to the URL that you have indicated. For example, if you specified http://localhost/PhotoViwer/ as the publishing directory and your web publishing directory is C:\Inetpub\wwwroot\, then the virtual directory PhotoViewer will be mapped to the local path C:\Inetpub\wwwroot\PhotoViewer\.
Two types of files will be created under the C:\Inetpub\wwwroot\PhotoViewer directory:
□ Application Manifest
□ Deployment Manifest
The next two sections take a closer look at these two types of files.
Application Manifest
When you publish your application, three files and a folder are created in the publishing directory (see Figure 16-30):
□ Application Files— Folder containing the deployment files.
□ A publish.htm
web page— This contains instructions on how to install the application.
□ Application manifest— PhotoViewer.application
. This is the file that is referenced by the publish.htm file. An application manifest is an XML file that contains detailed information about the current application as well as its version number. Chapter 15 has more about application manifests.
□ setup.exe
— A setup application that installs the application onto the target computer.
Figure 16-30
Читать дальше