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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/815340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:17:01  来源:igfitidea点击:

How do I get a list of installed updates and hotfixes?

c#.netwindowslist

提问by Tamara Wijsman

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...

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

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.

但这似乎并没有列出所有内容,它只列出了 QFE。

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

我需要它在 Windows XP、Vista 和 7 上运行。

采纳答案by VolkerK

You can use IUpdateSession3::QueryHistory Method.
The properties of the returned entries are described at http://msdn.microsoft.com/en-us/library/aa386400(VS.85).aspx

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

Set updateSearch = CreateObject("Microsoft.Update.Session").CreateUpdateSearcher
Set updateHistory = updateSearch.QueryHistory(1, updateSearch.GetTotalHistoryCount)

For Each updateEntry in updateHistory
  Wscript.Echo "Title: " & updateEntry.Title
  Wscript.Echo "application ID: " & updateEntry.ClientApplicationID
  Wscript.Echo " --"
Next

edit: also take a look at http://msdn.microsoft.com/en-us/library/aa387287%28VS.85%29.aspx

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

回答by Tamara Wijsman

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

在进一步搜索我之前发现的内容之后。(是的,和 VolkerK 首先建议的一样)

  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.
  1. 在 %SystemRoot%\System32\ 中的 VS2008 CMD 下运行命令来获取托管 dll:
    tlbimp.exe wuapi.dll /out=WUApiInterop.dll
  2. 添加 WUApiInterop.dll 作为项目引用,以便我们查看函数。

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

使用以下代码,我可以获得一个列表,从中可以提取 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);

回答by Stein ?smul

Just in case you just want the list of updates and don't care if you get it via code or a GUI, here is how to do it in Powershell:

以防万一您只想要更新列表而不关心您是通过代码还是 GUI 获取它,这里是如何在 Powershell 中执行此操作:

  1. Open PowerShell (preferably "run as admin")
  2. Type "get-hotfix" and hit enter. That's it.
  1. 打开 PowerShell(最好“以管理员身份运行”)
  2. 输入“get-hotfix”并按回车键。就是这样。

Get hotfixes

获取修补程序

回答by Justin Trantham

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

Above is a simple extract string method I use to find that KB is in the security package like Tom Wijsman had mentioned and run his.

上面是一个简单的提取字符串方法,我用来找到 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);
}

this would get you the KB number like you're looking for I believe

我相信这会为您提供您正在寻找的 KB 编号

回答by Souvik Biswas

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

This will populate the listbox with currently installed Hotfixes or Updates

这将使用当前安装的修补程序或更新填充列表框

If you want to list all history of updates and hotfixes to show then, the above example of Tom Wijsman as stated will work

如果您想列出所有更新和修补程序的历史记录,那么上面提到的 Tom Wijsman 示例将起作用