This is a mirror of official site: http://jasper-net.blogspot.com/

Install Windows Updates using C# & WUAPI

| Wednesday, February 9, 2011
This document explains how to get the windows updates available for the current machine and selectively install them, using c#.

Overview
This document explains how to get the windows updates available for the current machine
and selectively install them, using c#.

Requirement
Perform Windows Updates customized installation using c# and Windows Update API Library.
This will provide centralized command over customized installation of updates, patches, KB for
Windows.
EX: If particular updates are not to be installed on some machines, in such cases, we can
configure our custom update application to ignore those updates, using this approach.

Benefits
1.  Exclusive command over the updates installed over a network of systems.
2.  Easy to develop and support, using .NET
3.  Simple API approach

References Required
Add reference to the c:\WINDOWS\system32\wuapi.dll file. This is the API library for Window
Update system.
Write the below import statement

using WUApiLib;
using System.Management;


Coding

Now, we need to search the Microsoft updates for this machine;

UpdateSessionClass uSession = new UpdateSessionClass();
IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher();
ISearchResult uResult = uSearcher.Search("IsInstalled=0 and
Type='Software'");

All the updates found will be now populated into the uResult collection object, which can be
accessed using the below foreach loop

foreach (IUpdate update in uResult.Updates)
{
      Console.WriteLine(update.Title);
}

Note that, we have only found the updates available for this machine, but haven’t
downloaded them yet.

You can iterate the available updates to select only the required updates and add then to an
UpdateCollection class which can be assigned to the below UpdateDownloader class so as to
download them

Now we have to create an UpdateDownloader class object to download the updates, as below

UpdateDownloader downloader = uSession.CreateUpdateDownloader();
downloader.Updates = uResult.Updates;
downloader.Download();


Read more: EggCafe

Posted via email from Jasper-net