C#使用WMI獲取控制面板中安裝的所有程序列表
WMI
全稱Windows Management Instrumentation,Windows Management Instrumentation是Windows中用于提供共同的界面和對象模式以便訪問有關操作系統、設備、應用程序和服務的管理信息。如果此服務被終止,多數基于 Windows 的軟件將無法正常運行。如果此服務被禁用,任何依賴它的服務將無法啟動。
WMI提供公用接口及對象模型,以存取有關操作系統、裝置、應用程序及服務的管理信息。
新建窗體應用程序WindowsManagementDemo,將默認的Form1重命名為FormWMI,
添加引用System.Management,如下圖:
窗體FormWMI設計器代碼如下:
文件FormWMI.Designer.cs
namespace WindowsManagementDemo
{partial class FormWMI{/// <summary>/// 必需的設計器變量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// 清理所有正在使用的資源。/// </summary>/// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗體設計器生成的代碼/// <summary>/// 設計器支持所需的方法 - 不要修改/// 使用代碼編輯器修改此方法的內容。/// </summary>private void InitializeComponent(){this.dgvWMI = new System.Windows.Forms.DataGridView();this.rtxtMessage = new System.Windows.Forms.RichTextBox();this.Column6 = new System.Windows.Forms.DataGridViewTextBoxColumn();this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();this.Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();((System.ComponentModel.ISupportInitialize)(this.dgvWMI)).BeginInit();this.SuspendLayout();// // dgvWMI// this.dgvWMI.AllowUserToAddRows = false;this.dgvWMI.AllowUserToDeleteRows = false;this.dgvWMI.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;this.dgvWMI.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {this.Column6,this.Column1,this.Column2,this.Column3,this.Column4,this.Column5});this.dgvWMI.Location = new System.Drawing.Point(12, 12);this.dgvWMI.MultiSelect = false;this.dgvWMI.Name = "dgvWMI";this.dgvWMI.ReadOnly = true;this.dgvWMI.RowHeadersWidth = 25;this.dgvWMI.RowTemplate.Height = 23;this.dgvWMI.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;this.dgvWMI.Size = new System.Drawing.Size(1084, 363);this.dgvWMI.TabIndex = 0;// // rtxtMessage// this.rtxtMessage.Location = new System.Drawing.Point(12, 381);this.rtxtMessage.Name = "rtxtMessage";this.rtxtMessage.ReadOnly = true;this.rtxtMessage.Size = new System.Drawing.Size(1084, 395);this.rtxtMessage.TabIndex = 1;this.rtxtMessage.Text = "";// // Column6// this.Column6.HeaderText = "序號";this.Column6.Name = "Column6";this.Column6.ReadOnly = true;this.Column6.Width = 80;// // Column1// this.Column1.HeaderText = "名稱";this.Column1.Name = "Column1";this.Column1.ReadOnly = true;this.Column1.Width = 370;// // Column2// this.Column2.HeaderText = "發布者";this.Column2.Name = "Column2";this.Column2.ReadOnly = true;this.Column2.Width = 200;// // Column3// this.Column3.HeaderText = "安裝時間";this.Column3.Name = "Column3";this.Column3.ReadOnly = true;// // Column4// this.Column4.HeaderText = "安裝路徑";this.Column4.Name = "Column4";this.Column4.ReadOnly = true;this.Column4.Width = 180;// // Column5// this.Column5.HeaderText = "版本";this.Column5.Name = "Column5";this.Column5.ReadOnly = true;this.Column5.Width = 120;// // FormWMI// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(1108, 788);this.Controls.Add(this.rtxtMessage);this.Controls.Add(this.dgvWMI);this.Name = "FormWMI";this.Text = "WMI(Windows Management Instrumentation)用于提供共同的界面和對象模式以便訪問有關操作系統、設備、應用程序和服務的管理信息";this.Load += new System.EventHandler(this.FormWMI_Load);((System.ComponentModel.ISupportInitialize)(this.dgvWMI)).EndInit();this.ResumeLayout(false);}#endregionprivate System.Windows.Forms.DataGridView dgvWMI;private System.Windows.Forms.RichTextBox rtxtMessage;private System.Windows.Forms.DataGridViewTextBoxColumn Column6;private System.Windows.Forms.DataGridViewTextBoxColumn Column1;private System.Windows.Forms.DataGridViewTextBoxColumn Column2;private System.Windows.Forms.DataGridViewTextBoxColumn Column3;private System.Windows.Forms.DataGridViewTextBoxColumn Column4;private System.Windows.Forms.DataGridViewTextBoxColumn Column5;}
}
窗體FormWMI代碼如下
文件FormWMI.cs.
[因讀取程序遍歷數據過多,這里使用等待任務await Task]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;namespace WindowsManagementDemo
{public partial class FormWMI : Form{public FormWMI(){InitializeComponent();/** 使用WMI,需要添加System.Management的引用* 在C#中,可以使用Windows Management Instrumentation (WMI) 來獲取控制面板中安裝的所有程序列表。* 以下是一個簡單的示例代碼,展示了如何使用WMI查詢獲取這些信息:* Windows Management Instrumentation是Windows中用于提供共同的界面和對象模式以便訪問有關操作系統、設備、應用程序和服務的管理信息。* 如果此服務被終止,多數基于 Windows 的軟件將無法正常運行。如果此服務被禁用,任何依賴它的服務將無法啟動。* WMI提供公用接口及對象模型,以存取有關操作系統、裝置、應用程序及服務的管理信息。*/dgvWMI.Rows.Clear();}/// <summary>/// 異步顯示文本內容/// </summary>/// <param name="message"></param>private void DisplayMessage(string message) {if (!IsHandleCreated) {return;}this.BeginInvoke(new Action(() => {if (rtxtMessage.TextLength >= 40960) {rtxtMessage.Clear();}rtxtMessage.AppendText($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}->{message}\n");rtxtMessage.ScrollToCaret();}));}private async void FormWMI_Load(object sender, EventArgs e){await Task.Run(() => {ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Product");ManagementObjectCollection managementCollection = searcher.Get();DisplayMessage("累計個數:" + managementCollection.Count);int sequence = 0;int tempIndex = 0;foreach (ManagementBaseObject install in managementCollection){sequence++;this.BeginInvoke(new Action(() =>{tempIndex++;dgvWMI.Rows.Add(tempIndex, install["Name"], install["Vendor"], install["InstallDate"], install["InstallLocation"], install["Version"]); }));PropertyDataCollection Properties = install.Properties;DisplayMessage($"【{sequence.ToString("D3")}】{install["Name"]}:屬性鍵值對個數:{install.Properties.Count}");foreach (PropertyData propertyData in Properties){DisplayMessage($"\x20\x20Name:{propertyData.Name},Value:{propertyData.Value},Type:{propertyData.Type}");}}});}}
}