WMI tester

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Net.Security;
using System.Xml.Serialization;
using System.Collections;
using System.Xml;
using System.Diagnostics;
using System.Management;

namespace DemoCodeTester
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string getWMIResult(string sql)
        {
            try
            {
                var sb = new StringBuilder();
                var index = 1;

                ManagementObjectSearcher searcher = new ManagementObjectSearcher(sql);
                ManagementObjectCollection objects = searcher.Get();
                foreach (ManagementObject obj in objects)
                {
                    sb.Append(index++.ToString() + ".*****************************************************").AppendLine();

                    try
                    {
                        var properties = obj.Properties.Cast<PropertyData>();
                        foreach (var p in properties)
                        {
                            if (p.Value != null)
                                sb.AppendFormat("{0}:{1}", p.Name, p.Value).AppendLine();
                        }
                    }
                    catch
                    {

                    }
                }

                return sb.ToString();
            }
            catch(Exception ex)
            {
                return ex.ToString();
            }
            finally
            {

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            var result = getWMIResult(txtWMI.Text);
            txtResult.Text = result;
        }
    }
}
namespace DemoCodeTester
{
    partial class Form1
    {
        /// <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.btnExecute = new System.Windows.Forms.Button();
            this.txtResult = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.txtWMI = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // btnExecute
            // 
            this.btnExecute.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnExecute.Location = new System.Drawing.Point(351, 8);
            this.btnExecute.Name = "btnExecute";
            this.btnExecute.Size = new System.Drawing.Size(75, 23);
            this.btnExecute.TabIndex = 0;
            this.btnExecute.Text = "Execute";
            this.btnExecute.UseVisualStyleBackColor = true;
            this.btnExecute.Click += new System.EventHandler(this.btnExecute_Click);
            // 
            // txtResult
            // 
            this.txtResult.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtResult.Location = new System.Drawing.Point(12, 37);
            this.txtResult.Multiline = true;
            this.txtResult.Name = "txtResult";
            this.txtResult.Size = new System.Drawing.Size(414, 261);
            this.txtResult.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(13, 13);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(29, 12);
            this.label1.TabIndex = 2;
            this.label1.Text = "WMI:";
            // 
            // txtWMI
            // 
            this.txtWMI.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.txtWMI.Location = new System.Drawing.Point(48, 10);
            this.txtWMI.Name = "txtWMI";
            this.txtWMI.Size = new System.Drawing.Size(297, 21);
            this.txtWMI.TabIndex = 3;
            this.txtWMI.Text = "select * from Win32_PhysicalMedia";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(438, 310);
            this.Controls.Add(this.txtWMI);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtResult);
            this.Controls.Add(this.btnExecute);
            this.Name = "Form1";
            this.Text = "WMI Tester";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button btnExecute;
        private System.Windows.Forms.TextBox txtResult;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox txtWMI;
    }
}

 常用类:

Win32_Processor                        // CPU 处理器

Win32_PhysicalMemory                   // 物理内存

Win32_Keyboard                         // 键盘

Win32_PointingDevice                   // 点输入设备,如鼠标

Win32_DiskDrive                        // 硬盘驱动器

Win32_CDROMDrive                       // 光盘驱动器

Win32_BaseBoard                        // 主板

Win32_BIOS                             // BIOS 芯片

Win32_ParallelPort                     // 并口

Win32_SerialPort                       // 串口

Win32_SoundDevice                      // 多媒体设置

Win32_USBController                    // USB 控制器

Win32_NetworkAdapter                   // 网络适配器

Win32_NetworkAdapterConfiguration      // 网络适配器设置

Win32_Printer                          // 打印机

Win32_PrinterConfiguration             // 打印机设置

Win32_PrintJob                         // 打印机任务

Win32_TCPIPPrinterPort                 // 打印机端口

Win32_POTSModem                        // MODEM

Win32_POTSModemToSerialPort            // MODEM 端口

Win32_DesktopMonitor                   // 显示器

Win32_VideoController                  // 显卡细节。

Win32_VideoSettings                    // 显卡支持的显示模式。

Win32_TimeZone                         // 时区

Win32_SystemDriver                     // 驱动程序

Win32_DiskPartition                    // 磁盘分区

Win32_LogicalDisk                      // 逻辑磁盘

Win32_LogicalMemoryConfiguration       // 逻辑内存配置

Win32_PageFile                         // 系统页文件信息

Win32_PageFileSetting                  // 页文件设置

Win32_BootConfiguration                // 系统启动配置

Win32_OperatingSystem                  // 操作系统信息

Win32_StartupCommand                   // 系统自动启动程序

Win32_Service                          // 系统安装的服务

Win32_Group                            // 系统管理组

Win32_GroupUser                        // 系统组帐号

Win32_UserAccount                      // 用户帐号

Win32_Process                          // 系统进程

Win32_Thread                           // 系统线程

Win32_Share                            // 共享

Win32_NetworkClient                    // 已安装的网络客户端

Win32_NetworkProtocol                  // 已安装的网络协议

微软官方类参考: https://msdn.microsoft.com/en-us/library/aa394084%28VS.85%29.aspx

posted on 2019-03-19 10:13  空明流光  阅读(123)  评论(0编辑  收藏  举报

导航