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

C# 如何获取已安装更新和修补程序的列表?,c#,.net,windows,list,C#,.net,Windows,List,已安装在我的计算机上的每个更新和修补程序的列表,来自Microsoft Windows update或知识库。我需要每个人的ID以KBxxxxxx或类似的形式表示 目前我有: const string query = "SELECT HotFixID FROM Win32_QuickFixEngineering"; var search = new ManagementObjectSearcher(query); var collection = search.Get(); foreach (

已安装在我的计算机上的每个更新和修补程序的列表,来自Microsoft Windows update或知识库。我需要每个人的ID以KBxxxxxx或类似的形式表示

目前我有:

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上工作。

您可以使用。
返回项的属性在中进行了说明

设置updateSearch=CreateObject(“Microsoft.Update.Session”).CreateUpdateSearcher 设置updateHistory=updateSearch.QueryHistory(1,updateSearch.GetTotalHistoryCount) 对于updateHistory中的每个updateEntry Wscript.Echo“Title:&updateEntry.Title Wscript.Echo“应用程序ID:&updateEntry.ClientApplicationID Wscript.Echo“-” 下一个
编辑:在进一步搜索我之前找到的内容之后,再看一看。(是的,与沃尔克首先建议的相同)

  • 在%SystemRoot%\System32\中的VS2008 CMD下运行命令以获取托管dll:
    tlbimp.exe wuapi.dll/out=WUApiInterop.dll
  • 添加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);
    
    var updateSession=new updateSession();
    var updatesarcher=updateSession.createUpdatesarcher();
    var count=updateSearcher.GetTotalHistoryCount();
    var history=updatesarcher.QueryHistory(0,计数);
    对于(int i=0;i
    以防您只需要更新列表,而不关心是否通过代码或GUI获得更新,下面是如何在Powershell中执行此操作:

  • 打开PowerShell(最好是“以管理员身份运行”)
  • 键入“获取修补程序”并按enter键。就这样
  • 上面是一个简单的提取字符串方法,我使用它来发现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);
    }
    
    var updateSession=new updateSession();
    var updatesarcher=updateSession.createUpdatesarcher();
    var count=updateSearcher.GetTotalHistoryCount();
    var history=updatesarcher.QueryHistory(0,计数);
    对于(int i=0;i
    我相信这会让你得到你想要的KB号码

    const string querys = "SELECT HotFixID FROM Win32_QuickFixEngineering";
    var search = new ManagementObjectSearcher(querys);
    var collection = search.Get();
    
    foreach (ManagementObject quickfix in collection)
    {
        hotfix = quickfix["HotFixID"].ToString();
    }
    
    listBox1.Items.Add(hotfix);
    
    这将使用当前安装的修补程序或更新填充列表框

    如果要列出要显示的所有更新和修补程序的历史记录
    然后,上面提到的Tom Wijsman示例将起作用

    不幸的是,如果其中一个更新已卸载,它仍将显示在此列表中。请查看操作属性可以找到
    updateEntry
    中所有属性的列表。不确定为什么会出现在这里,因为它不是,应该有自己的问题和答案。不知道,我认为它会反映Windows Update中看到的历史;但可能是错的,我建议你做一个原型,看看它能做什么。考虑到我现在正在运行Gentoo Linux,我目前没有可用的Windows计算机。如何在远程计算机上创建
    UpdateSession
    的实例?今天,在Windows 10 Professional上安装Windows update[KB3156421、KB890830和KB3157993](我想KB3156421是问题所在);查询历史记录[i]。ResultCode引发comException:Exception from HRESULT:0x80240FFF,我已为添加了一个try/catchthis@buildcomplete:似乎更像是WSUS问题,请咨询更多信息information@TomWijsman:我必须承认,我对windows update等不太了解,但是:1)安装的更新都是由windows update自动安装的,我们没有安装更新的内部服务器。2) 我们只在其中一台出现故障的机器上安装了“一个”程序(该程序出现故障)。3) 我可以查询一些对象,但只有在读取了一些对象后才会失败。
    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);
    }
    
    const string querys = "SELECT HotFixID FROM Win32_QuickFixEngineering";
    var search = new ManagementObjectSearcher(querys);
    var collection = search.Get();
    
    foreach (ManagementObject quickfix in collection)
    {
        hotfix = quickfix["HotFixID"].ToString();
    }
    
    listBox1.Items.Add(hotfix);