运行结果截图:
代码:
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.Management.Instrumentation;
using System.Management;
namespace Eyuan.WMIStudy.Win32
{
public partial class fmDesktop : Form
{
public fmDesktop()
{
InitializeComponent();
}
#region 类字段
private StringBuilder sbDesktopInfo = new StringBuilder();//存储获取的信息
private string strSql = string.Empty;//查询语句
private int iMOCount;//获取的ManagementObject个数
private string mopName;//ManagementObject属性名称
#endregion
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void fmDesktop_Load(object sender, EventArgs e)
{
lbDesktop.HorizontalScrollbar = true;
//查询所有的Win32_Desktop管理对象实例的所有属性、方法
strSql = "SELECT * FROM Win32_Desktop";
ManagementObjectSearcher search = new ManagementObjectSearcher(strSql);
iMOCount = 0;
//
ManagementObjectCollection moc=search.Get();
foreach (ManagementObject mo in moc)
{
sbDesktopInfo.AppendLine("第" + (++iMOCount) + "个Desktop:");
//获取ManagementObject的属性的枚举
PropertyDataCollection.PropertyDataEnumerator pde = mo.Properties.GetEnumerator();
while (pde.MoveNext())
{
mopName = pde.Current.Name;
sbDesktopInfo.AppendFormat("{0,-15}:{1}\n", mopName, mo[mopName]);
}
sbDesktopInfo.AppendLine("");
}
//
foreach (string strDesktopInfo in sbDesktopInfo.ToString().Split(new string[] { "\n" }, StringSplitOptions.None))
{
lbDesktop.Items.Add(strDesktopInfo);
}
}
}
}