Use WUA to determine the installation status of Windows updates

Page update date :
Page creation date :

summary

Use WUA to check for updates (in KB) that are installed on the Windows you are currently using.

KB がインストールされている一覧を表示しています。

Operating Environment

Operation check environment

Windows Version
  • Windows 7 Ultimate
.NET Framework Version
  • 4

System requirements required

Windows Version
  • Windows 7 (other environments unconfirmed)
.NET Framework Version
  • 4.0 (no other environments confirmed)

substance

About this sample

The goal of this sample is to determine the KB that is installed on Windows, and the purpose is almost the same as "Use WMI to determine the installation status of Windows updates." However, this sample is checked using "WUA" instead of WMI.

The KB description is described in "Use WMI to find out how Windows updates are installed," so check there.

What is WUA?

WUA stands for "Windows Update Agent" and is a set of COM interfaces that allow access to Windows Update and Windows Server Update Services (WSUS). You can use it to determine which KB is installed on Windows.

For a detailed explanation of WUA and programming with WUA, see the following links:

Programs that search for KB installation status

Adding References (C#)

「参照設定」を右クリックして「参照の追加」を選択

To use WUA, you must reference the WUAPI 2.0 Type Library from COM.

For C# projects, right-click References from Solution Explorer and choose Add References.

参照の追加ダイアログで WUAPI 2.0 Type Library を選択して追加しています

When the Add Reference dialog appears, select WUAPI 2.0 Type Library from the COM tab and click the OK button.

OK if "WUApiLib" is added to the Solution Explorer reference settings.

Add a reference (VB.NET)

My Project から開くを選択

For VB.NET, right-click My Project from Solution Explorer and choose Open.

プロジェクトのプロパティから参照タブを選択し WUAPI 2.0 Type Library の参照を追加しています

When the properties open, click "Browse" from the tab on the left, click the Add button on the right. As in C#, the Add Reference dialog is displayed, so select "WUAPI 2.0 Type Library" from the "COM" tab and click the OK button.

WUAPI 2.0 Type Library が追加されていることを確認し、WUApiLib 名前空間をインポートします

If you return to the previous screen and add "WUAPI 2.0 Type Library" to the middle list, it is OK.

Also, let's check "WUApiLib" from the list below to omit the description of the namespace in the program.

scene

KB チェック実行ボタンと検索結果一覧を表示するためのテキストボックスを配置

The screen used in this sample is a simple screen with an execution button to check the installation status of the KB and a text box that displays the results.

I'm making it in WPF, but the same screen is configurable in Windows Form.

program

C UpdateSession # adds ""using WUApiLib; to the beginning of the code to reduce the description of classes and other namespaces. In VB.NET, you specified to import the namespace in the project properties, so you don't need to write anything special, but if you haven't, you need to add ".Imports WUApiLib

* In both C# and VB.NET, if all class names are written from a namespace such as ",WUApiLib.UpdateSession the above specification is not necessary.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WUApiLib; // 参照から「COM」より「WUAPI 2.0 Type Library」追加

Below is the code that searches and lists the installed KB.

C #

this.ResultTextBox.Text = "";

// アップデートセッション 作成
UpdateSession us = new UpdateSession();

// アップデート検索インスタンス作成
IUpdateSearcher searcher = us.CreateUpdateSearcher();

// 「インストールされているもの」「ソフトウェア」で検索し、結果を取得
ISearchResult result = searcher.Search("IsInstalled=1 and Type='Software'");

StringBuilder builder = new StringBuilder();

// アップデート一覧からタイトル一覧を取得する。
foreach (IUpdate u in result.Updates)
{
  builder.AppendLine("[" + u.Title + "]");
}

builder.AppendLine();

// アップデート一覧から KB の番号だけ取得する。
foreach (IUpdate u in result.Updates)
{
  foreach (string str in u.KBArticleIDs)
  {
    builder.AppendLine(str);
  }
}

// 取得した KB 一覧をセット
this.ResultTextBox.Text = builder.ToString();

VB.NET

Me.ResultTextBox.Text = ""

' アップデートセッション 作成
Dim us As New UpdateSession()

' アップデート検索インスタンス作成
Dim searcher As IUpdateSearcher = us.CreateUpdateSearcher()

' 「インストールされているもの」「ソフトウェア」で検索し、結果を取得
Dim result As ISearchResult = searcher.Search("IsInstalled=1 and Type='Software'")

Dim builder As New System.Text.StringBuilder()

' アップデート一覧からタイトル一覧を取得する。
For Each u As IUpdate In result.Updates
  builder.AppendLine("[" + u.Title + "]")
Next

builder.AppendLine()

' アップデート一覧から KB の番号だけ取得する。
For Each u As IUpdate In result.Updates
  For Each str As String In u.KBArticleIDs
    builder.AppendLine(str)
  Next
Next

' 取得した KB 一覧をセット
Me.ResultTextBox.Text = builder.ToString()

The content is as commented.

IUpdateSearcher.SearchYou can change what you get by the string you specify as the argument of the method. For more information, see IUpdateSearcher::Search Method.