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