tags:

views:

87

answers:

0

How can I query for an accurate and localized list of Windows updates installed on a machine using C#?

I define accurate as matching what is displayed in the "Microsoft Windows" category of Microsoft's View Installed Updates dialog under Programs and Features in Windows 7.

If I use WUApi.DLL, the information is returned localized but I can't get an accurate list. In the case of WUApi.dll, some hotfixes are missing and if an update has been uninstalled, it still shows up in the list generated by the following code:

public static void GetWindowsUpdates() 
{ 
    var updateSession = new UpdateSession(); 
    var updateSearcher = updateSession.CreateUpdateSearcher(); 
    var count = updateSearcher.GetTotalHistoryCount(); 
    if (count == 0) 
        return; 

    var history = updateSearcher.QueryHistory(0, count); 
    for (int i = 0; i < count; i++) 
    { 
        if (history[i].ResultCode == OperationResultCode.orcSucceeded) 
        { 
            Console.WriteLine(history[i].Title); 

            if (history[i].Operation == UpdateOperation.uoUninstallation) 
            { 
                Console.WriteLine("!!! Operation == uninstall"); // This is never true 
            } 
        } 
    } 
} 

The WUApi search method also didn't provide an accurate list using the following code:

        WUApiLib.UpdateSessionClass session = new WUApiLib.UpdateSessionClass(); 
        WUApiLib.IUpdateSearcher searcher = session.CreateUpdateSearcher(); 

        searcher.IncludePotentiallySupersededUpdates = true; 

        WUApiLib.ISearchResult result = searcher.Search("IsInstalled=1"); 
        Console.WriteLine("Updates found: " + result.Updates.Count); 
        foreach (IUpdate item in result.Updates) 
        { 
            Console.WriteLine(item.Title); 
        } 

If I use WMI to read the list of updates, I can get an accurate list, but it is not localized. I use the following code:

ManagementObjectSearcher searcher = new ManagementObjectSearcher(new ObjectQuery("select * from Win32_QuickFixEngineering")); 
searcher.Options.UseAmendedQualifiers = true; 
searcher.Scope.Options.Locale = "MS_" + CultureInfo.CurrentCulture.LCID.ToString("X"); 
ManagementObjectCollection results = searcher.Get(); 

Console.WriteLine("\n==WMI==" + results.Count); 
foreach (ManagementObject item in results) 
{ 
    Console.WriteLine("\t--Properties--"); 
    foreach (var x in item.Properties) 
    { 
        Console.WriteLine(x.Name + ": " + item[x.Name]); 
    } 
    Console.WriteLine("\t--System Properties--"); 
    foreach (var x in item.SystemProperties) 
    { 
        Console.WriteLine(x.Name + ": " + x.Value); 
    } 
    Console.WriteLine("\t--Qualifiers--"); 
    foreach (var x in item.Qualifiers) 
    { 
        Console.WriteLine(x.Name + ": " + x.Value); 
    } 
}