查询已安装的Windows更新
问题描述:
的准确和本地化的名单我如何可以查询Windows更新安装使用C#的机器上的准确和局部列表?查询已安装的Windows更新
我定义准确作为匹配什么是显示在微软看来下安装程序和功能更新对话框的“的Microsoft Windows”类别中的Windows 7
如果我使用WUAPI.DLL,信息返回本地化,但我无法得到一个准确的列表。在WUAPI.DLL的情况下,一些修补程序丢失,如果更新已被卸载,它仍然由下面的代码生成的列表中显示:
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
}
}
}
}
的WUApi搜索方法也没有提供使用下面的代码准确名单:
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);
}
如果我使用WMI来读取更新的列表中,我可以得到一个准确的名单,但没有本地化。我使用下面的代码:
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);
}
}
答
的WUApi只注册通过WUApi完成的动作,因此,如果您手动安装或删除更新它要么保留在列表中被卸载后,或者从来没有在列表中显示出来。因此,我认为WUApi不能作为一个准确的清单。
WMI允许访问Windows更新的准确列表,但该列表仅被过滤为“Microsoft Windows”类别。这很困难,因为我的要求是获取所有更新的列表。
“View Installed Updates”对话框内部使用CBS(基于组件的服务)。不幸的是,CBS并不公开。关于API的一些细节可以在这里找到: http://msdn.microsoft.com/en-us/library/Aa903048.aspx
我们在尝试获取所有更新时遇到同样的问题。你是如何解决这个问题的。如果可能的话,你可以分享代码吗? – 2010-12-30 19:28:39
msdn上有没有关于“WUApi只注册通过WUApi完成的操作”的文档? – 2010-12-30 19:32:31