C#使用WMI获取控制面板中安装的所有程序列表

C#使用WMI获取控制面板中安装的所有程序列表

WMI

全称Windows Management Instrumentation,Windows Management Instrumentation是Windows中用于提供共同的界面和对象模式以便访问有关操作系统、设备、应用程序和服务的管理信息。如果此服务被终止,多数基于 Windows 的软件将无法正常运行。如果此服务被禁用,任何依赖它的服务将无法启动。

WMI提供公用接口及对象模型,以存取有关操作系统、装置、应用程序及服务的管理信息。

新建窗体应用程序WindowsManagementDemo,将默认的Form1重命名为FormWMI,

添加引用System.Management,如下图:

窗体FormWMI设计器代码如下:

文件FormWMI.Designer.cs

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

        }

        #endregion

        private 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

cs 复制代码
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}");
                    }
                }
            });
            
        }
    }
}

运行如图:

相关推荐
死磕音视频7 小时前
让小模型做选择题:限定候选项并读取 logits 概率的入门实践(附 Python 代码)
开发语言·python·大模型·文本分类·transformers
顽疲9 小时前
Java vue 养老系统源码详解:月账单 care_month_bill 与 Quartz 扣费 Job 实践
java·开发语言·vue.js·vue养老·springboot养老·java养老
Wang's Blog10 小时前
Java 项目实战: 交友项目 - 安装开发工具与环境准备
java·开发语言·交友
曹牧10 小时前
C#:输入的字符串格式不正确
c#
程序喵大人11 小时前
【C++入门】编译链接模型 - 07 链接器怎样把符号对上号
开发语言·c++·编译器·编译链接·链接器
微三云 - 廖会灵 (私域系统开发)12 小时前
供应链系统实战:多渠道订单聚合与分布式库存一致性方案总结
开发语言·分布式·php
学逆向的12 小时前
C 数据溢出与精度丢失
c语言·开发语言·网络安全
霸道流氓气质13 小时前
AgentFlow 入门与实战:Task-Based 并发、EWMA 动态调度与四道门禁的 Java 生产级编排
java·开发语言
释厄62313 小时前
错误无误公理——错误正确本同源都迭代定律——逆转有价值
android·开发语言·kotlin
2601_9628857213 小时前
如何用 Python计算 CR能量指标(中间意愿指标)?
开发语言·python