Use WMI to determine the installation status of Windows updates
summary
Use WMI to check for updates (in KB) that are installed on the Windows you are currently using.
Operating Environment
Operation check environment
Windows Version |
|
.NET Framework Version |
|
System requirements required
Windows Version |
|
.NET Framework Version |
|
substance
What is KB?
KB is also used as an abbreviation for the expression "kilobyte" or "keyboard", so I don't think it's a word used for the general public, but updates such as Windows security support and bug fixes are sometimes referred to as "KB" after the first two letters of the ID.
Each of these updates is assigned an ID and specified in the format "KBXXXXXXX" where XXXXXX is any number. The number of digits is not fixed). Note, however, that not all updates follow this format.
By the way, the original name of this "KB" is "Microsoft Knowledge Base" and matches the ID of the technical article published by Microsoft on the Web. For example, if it is an update to KB980218, the corresponding technical article is http://support.microsoft.com/?kbid=980218".
Programs that search for KB installation status
Although the samples are described in Visual Studio 2010 and WPF, KB-checking programs can also be used in earlier Visual Studio, Windows Form, and console apps. In this article, we'll use Windows Management Instrumentation (WMI) to check the KB installation.
- Windows Management Instrumentation (from Wikipedia)
Adding References
To use WMI, you use the ManagementObjectSearcher and ManagementObject classes, but you must refer to System.Management .dll to use these classes.
For C# projects, right-click References from Solution Explorer and choose Add References.
When the Add Reference dialog appears, click on ". From the NET tab, select System.Management and click the OK button.
OK if "System.Management" is added to the Solution Explorer reference.
If VB.NET open the project properties, click "Browse" from the tab on the left, click the Add button on the right. As above, the Add Reference dialog is displayed, so select "System.Management" and click the OK button.
If you return to the previous screen and add "System.Management" to the middle list, it is OK.
Also, check "System.Management" from the list below to omit namespace descriptions in your program.
Also, although it is not related to the original tips, this sample uses the StringBuilder class, so I also checked the "System.Text" namespace. (VB.NET only)
scene
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 ManagementObjectSearcher
# adds ""using System.Management;
to the beginning of the code to reduce the description of classes and other namespaces. In VB.NET, you specified that the namespace should be imported in the project properties, so you don't need to write anything special, but if you haven't done so, you need to add ".Imports System.Management
* In both C# and VB.NET, if all class names are written from a namespace such as "System.Management.ManagementObjectSearcher
", the above specification is not necessary.
C #
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 System.Management; // 追加 (参照から「.NET」より「System.Management」追加)
Below is the code that searches and lists the installed KB.
C #
this.ResultTextBox.Text = "";
// WMI クエリをセットして管理オブジェクト検索クラスを作成
ManagementObjectSearcher query =
new ManagementObjectSearcher("Select * From Win32_QuickFixEngineering");
// WMI クエリを使用して管理オブジェクトコレクションを取得
ManagementObjectCollection queryCollection = query.Get();
StringBuilder builder = new StringBuilder();
// コレクションから管理オブジェクトを列挙する
// ここでは HotFixID を取得
foreach (ManagementObject mo in queryCollection)
{
builder.AppendLine(mo["HotFixID"].ToString());
}
// 取得した KB 一覧をセット
this.ResultTextBox.Text = builder.ToString();
VB.NET
Me.ResultTextBox.Text = ""
' WMI クエリをセットして管理オブジェクト検索クラスを作成
Dim query As ManagementObjectSearcher = _
New ManagementObjectSearcher("Select * From Win32_QuickFixEngineering")
' WMI クエリを使用して管理オブジェクトコレクションを取得
Dim queryCollection As ManagementObjectCollection = query.Get()
Dim builder As System.Text.StringBuilder = New System.Text.StringBuilder()
' コレクションから管理オブジェクトを列挙する
' ここでは HotFixID を取得
For Each mo As ManagementObject In queryCollection
builder.AppendLine(mo("HotFixID").ToString())
Next
' 取得した KB 一覧をセット
Me.ResultTextBox.Text = builder.ToString()
The ""ResultTextBox
in the first line is a text box for displaying the obtained results.
createsnew ManagementObjectSearcher
an instance of the managed object's search class and sets the WMI query as an argument. The syntax of a query is almost equal to an SQL statement, so look at it in relation to SQL for details. "Win32_QuickFixEngineering" represents an update that applies to the current operating system.
Performs a search on the WMI query specified earlier in "query.Get()
and returns the results as ".ManagementObjectCollection
Since "" lists the update information, we get "" in foreach and write a unique identifier from the "HotFixID"ManagementObjectCollection
ManagementObject
parameter as a result.
When you do this, the update IDs are listed as shown in the figure.
As you can see when you run it, it is slow only at the first run. After the second time, it is reasonably early.
It is not clear whether System.Management.dll is slow to load and process or WMI query execution is slow, but the fact that it is slow does not change, so if you are worried about latency when using it, I think that it is necessary to take measures such as asynchronous execution.