3. Writing the Update Code
Once you know whether the user's system is healthy
from an update check and download perspective, you need to know which
updates the user has actually installed. For example, you might worry
that your application won't work properly without the daylight saving
time and time zone updates found in the Knowledge Base article at http://support.microsoft.com/kb/2158563. Using the technique shown in Listing 2, you can obtain the complete list of updates and search it for the appropriate update.
Example 2. Obtaining a list of installed updates
private void btnUpdates_Click(object sender, EventArgs e) { // Clear the list box. lstResults.Items.Clear();
// Create the Microsoft Update Searcher (MUS) type. Type MUStype = Type.GetTypeFromProgID("Microsoft.Update.Searcher");
// Use the MUS type to create a searcher object. dynamic Searcher = Activator.CreateInstance(MUStype);
// Access the entire history. dynamic Updates = Searcher.QueryHistory(0, Searcher.GetTotalHistoryCount());
// Check each update in the history in turn. foreach (dynamic Update in Updates) { // Display only the successful updates. if (Update.ResultCode == 2) { // Display the update information.
lstResults.Items.Add("Title: " + Update.Title); lstResults.Items.Add("Description: " + Update.Description); lstResults.Items.Add("Date (GMT): " + Update.Date); lstResults.Items.Add("Installation Method: " + Update.ClientApplicationID);
// Add a blank line. lstResults.Items.Add(""); } } }
|
The code begins by clearing the previous results from the list box and creating a Type, MUStype, based on Microsoft.Update.Searcher. The code then creates the Search object using Activator.CreateInstance() with MUStype as an argument.
The Searcher object actually has access to
a number of methods for querying the update database. The example uses
the simplest of these techniques, QueryHistory(). You supply the starting record and number of records to return as inputs. The other methods are documented at http://msdn.microsoft.com/library/aa386530.aspx.
The next step is determining which updates to check. One of the most important properties for developers is the ResultCode. The example looks for updates that succeeded. A result code can have any of the following values:
2: The update succeeded.
4: The update failed for some reason other than that the user or system aborted it.
5:
The user or the system aborted the update. An update is aborted either
manually or when the system detects an error in the update code or
conditions.
Other:
There could be other update codes that aren't listed. A third-party
vendor could provide an update code for a partial update or a pending
update. In most cases, you aren't concerned about these alternate
conditions and need to know only whether the update succeeded, failed,
or aborted.
The output information also includes an
update title, a description of what the update does, the date the user
installed the update, and the technique used to install the update. Figure 3 shows typical output from this part of the example.