On-Demand Updates with ClickOnce

ClickOnce supports both automatic updating and on-demand updates. The default model checks for updates automatically at application launch (when connected), and applies those updates immediately. There are a number of options available for installed applications (available online and offline), including whether to check automatically at all, whether to do it on a background thread, whether to do it on a timed interval, and other options.



However, sometimes you may want to do updates on demand, either as the only update model, or in combination with automatic updating in the background on a periodic interval. To do that, you will need to use the ClickOnce API defined in the System.Deployment framework assembly.



The main class you will use to support on-demand updates is the ApplicationDeployment class defined in the System.Deployment.Application. You will typically add code in response to a user action (such as selecting a Check For Updates menu item) that goes out and checks to see if updates are available, and if so retrieves them. You will then need to restart the application to have those changes applied.



A simple but common pattern to accomplish this is something like the following:



// First check to see if we are running in a ClickOnce context

if (ApplicationDeployment.IsNetworkDeployed)

{

// Get an instance of the deployment

ApplicationDeployment deployment = ApplicationDeployment.CurrentDeployment;



// Check to see if updates are available

if (deployment.CheckForUpdate())

{

DialogResult res = MessageBox.Show(“A new version of the 

application is available, do you want to update?”,

“Application Updater”, MessageBoxButtons.YesNo);

if (res DialogResult.Yes)

{

// Do the update

deployment.Update();

DialogResult res2 = MessageBox.Show(“Update complete, do you

want to restart the application to apply the update?”,

“Application Updater”, MessageBoxButtons.YesNo);

if (res2 DialogResult.Yes)

{

// Restart the application to apply the update

Application.Restart();

}

}

}

else

{

MessageBox.Show(“No updates available.”, “Application Updater”);

}

}

else

{

MessageBox.Show(“Updates not allowed unless you are launched through ClickOnce.”);

}



There are of course asynchronous versions of the CheckForUpdate and Update methods if you want to avoid blocking your UI while this happens.



The project settings you will need may not be completely apparent. The first important setting is that you need to change the update behavior of the ClickOnce deployment to stop automatically checking for updates if you are doing only on-demand updates. You do this through the Publish tab of the project properties settings, Updates button, shown in Figure 1.



!Publish settings

Figure 1: ClickOnce Update Behavior Settings access



When you click that button, the dialog in Figure 2 will show with default settings selected as shown.

Update Settings defaults



Figure 2: ClickOnce Update Settings Dialog



What you will want to do for a pure on-demand updates application is to uncheck the box that says the application should check for updates at the top. The other trick that is not apparent but required is that you have to specify an Update location at the bottom or you will get an obscure error message when you try to launch the application on the client. So you should set up the update settings like shown in Figure 3.

Modified update settings

Figure 3: ClickOnce On-Demand Update Settings



With those settings in place, when you publish your application out and the client launches it, they can invoke the code shown earlier to check for and apply updates on-demand.



If you wanted to combine on-demand updates with periodic background checking for updates, you can do that by leaving updates enabled, but you will need to select the option to check for updates after the application starts. You will then want to configure the frequency of checking using the options in the middle of the dialog.

**

**