views:

1061

answers:

2

A list of every update and hotfix that has been installed on my computer, coming from either Microsoft Windows Update or from the knowledge base. I need the ID of each in the form of KBxxxxxx or some similar representation...

Currently I have:

const string query = "SELECT HotFixID FROM Win32_QuickFixEngineering";
var search = new ManagementObjectSearcher(query);
var collection = search.Get();

foreach (ManagementObject quickFix in collection)
    Console.WriteLine(quickFix["HotFixID"].ToString());

But this does not seem to list everything, it only lists QFE's.

I need it to work on Windows XP, Vista and 7.

+2  A: 
VolkerK
Unfortunately if one of these updates has been uninstalled, it will still display in this list.
Todd Kobus
+1  A: 

After some further search on what I've found earlier. (Yes, the same as VolkerK suggests first)

  1. Under VS2008 CMD in %SystemRoot%\System32\ run a command to get a managed dll:
    tlbimp.exe wuapi.dll /out=WUApiInterop.dll
  2. Add WUApiInterop.dll as a project reference so we see the functions.

Using the following code I can get a list from which I can extract the KB numbers:

var updateSession = new UpdateSession();
var updateSearcher = updateSession.CreateUpdateSearcher();
var count = updateSearcher.GetTotalHistoryCount();
var history = updateSearcher.QueryHistory(0, count);

for (int i = 0; i < count; ++i)
    Console.WriteLine(history[i].Title);
TomWij