如何获取已安装的更新和修补程序的列表?

我的计算机上安装的每个更新和修补程序的列表,来自Microsoft Windows Update或来自知识库。 我需要每个KBxxxxxx或一些相似的表示forms的ID …

目前我有:

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()); 

但是这似乎并没有列出所有的东西,只是列出了QFE的。

我需要它在Windows XP,Vista和7上工作。

您可以使用IUpdateSession3 :: QueryHistory方法 。
返回的条目的属性在http://msdn.microsoft.com/en-us/library/aa386400(VS.85).aspx

 设置updateSearch = CreateObject(“Microsoft.Update.Session”)。CreateUpdateSearcher
设置updateHistory = updateSearch.QueryHistory(1,updateSearch.GetTotalHistoryCount)

对于updateHistory中的每个updateEntry
   Wscript.Echo“标题:”&updateEntry.Title
   Wscript.Echo“application ID:”&updateEntry.ClientApplicationID
   Wscript.Echo“ - ”
下一个 

编辑:也看看http://msdn.microsoft.com/en-us/library/aa387287%28VS.85%29.aspx

经过一些进一步的搜索之后我发现了什么。 (是的,像VolkerK一样首先提出)

  1. 在VS2008 CMD%SystemRoot%\ System32 \下运行一个命令来获得一个托管的dll:
    tlbimp.exe wuapi.dll /out=WUApiInterop.dll
  2. 添加WUApiInterop.dll作为项目引用,所以我们看到的功能。

使用下面的代码,我可以得到一个列表,我可以从中提取KB数字:

 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); 

以防万一你只是想要更新的列表,并不在乎如果你通过代码或GUI获取它,这里是如何在Powershell中做到这一点:

  1. 打开PowerShell(最好是“以管理员身份运行”)
  2. 输入“get-hotfix”并按回车。 而已。

获取修补程序

  string ExtractString(string s) { // You should check for errors in real-world code, omitted for brevity try { var startTag = "("; int startIndex = s.IndexOf(startTag) + startTag.Length; int endIndex = s.IndexOf(")", startIndex); return s.Substring(startIndex, endIndex - startIndex); } catch { return ("CNVFL"); } } 

上面是一个简单的提取字符串方法,我用它来发现KB是像Tom Wijsman提到的那样运行他的安全包。

 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){ //sets KB here!! string _splitstring = ExtractString(history[i].Title); Console.WriteLine(_splitstring); } 

这会让你的KB号码像你正在寻找我相信