C# - 从WUAPI获取全貌

时间:2017-07-26 15:32:25

标签: c# windows-update wuapi

我正在尝试在许多不同的计算机上收集Windows更新的准确图片,特别是KB安装。我已经尝试了许多不同的代码片段,我发现这些代码已经分散了,但我似乎还无法准确地了解所安装的内容。准确地说,我的意思是,当我使用Windows UI检查计算机上的Windows Update History时,我收集的内容似乎是所显示内容的一部分!似乎无法弄清楚这一点!

以下是我尝试过的一些事情;

        UpdateSession uSession = new UpdateSession();

        IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher();
        uSearcher.Online = false;

        ISearchResult sResult = uSearcher.Search("IsInstalled=1");

        foreach (IUpdate update in sResult.Updates)
        {
            foreach (string kbaid in update.KBArticleIDs)
            {
                txtAllUpdates.AppendText(kbaid + Environment.NewLine);
            }
        }

我还尝试在同一例程中添加代码来收集Bundled Updates字段中的所有更新,就像这样;

        foreach (IUpdate update2 in update.BundledUpdates)
        {
            txtAllUpdates.AppendText("\t--> " + update2.Title + Environment.NewLine);
            foreach (string kbaid2 in update2.BundledUpdates)
            {
                string kbNo = GetKBNo(update2.Title.ToLower());
                txtAllUpdates.AppendText("\t\t" + kbNo);
            }
        }

我也尝试查看更新历史记录,但这为我提供了另一组数据 - 仍未完成!

        UpdateSession updateSession = new UpdateSession();
        IUpdateSearcher updateSearcher = updateSession.CreateUpdateSearcher();
        int count = updateSearcher.GetTotalHistoryCount();
        MessageBox.Show("Total Count = " + count);
        IUpdateHistoryEntryCollection history = updateSearcher.QueryHistory(0, count);

        for (int i = 0; i < count; ++i)
        {
            txtAllUpdates.AppendText("\t\t\t" + history[i].Title);

        }

我还检查了一些利用注册表的代码,但从我读过的内容来看,这不是正确的做事方式。此时,我正在执行许多不同的查询,搜索“KB”引用的条目并构建列表并删除重复项,但我仍然没有得到我在屏幕上看到的相同列表!即使这确实有效,也不可能是正确的方法 - 我觉得我必须遗漏一些东西。

最后,我尝试获取有关上次检查和安装更新的信息 - 即使这与显示的内容不匹配。我用以下代码完成了这个;

        var auc = new AutomaticUpdatesClass();

        DateTime? lastInstallationSuccessDateUtc = null;
        if (auc.Results.LastInstallationSuccessDate is DateTime)
            lastInstallationSuccessDateUtc = new DateTime(((DateTime)auc.Results.LastInstallationSuccessDate).Ticks, DateTimeKind.Utc);

        DateTime? lastSearchSuccessDateUtc = null;
        if (auc.Results.LastSearchSuccessDate is DateTime)
            lastSearchSuccessDateUtc = new DateTime(((DateTime)auc.Results.LastSearchSuccessDate).Ticks, DateTimeKind.Utc);

        lblInstall.Text += lastInstallationSuccessDateUtc.ToString();
        lblSearch.Text += lastSearchSuccessDateUtc.ToString();

有没有人在这方面有一些专业知识?真的想要做到这一点!

感谢您花时间阅读!

尊敬, 马歇尔

0 个答案:

没有答案