mercredi, février 01, 2006

WMI Example : My computer SOME hardware's informations

this is a simple example using the WMI features, to execute it you need to add to your form an listView named "listViewResult", also the reference to System.management

using System.Management;

ManagementObjectSearcher query;
ManagementObjectCollection queryCollection;
System.Management.ObjectQuery oq;

ConnectionOptions co = new ConnectionOptions();
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\localhost\\root\\cimv2", co);

oq = new System.Management.ObjectQuery("SELECT * FROM Win32_ComputerSystem ");
query = new ManagementObjectSearcher(ms, oq);

queryCollection = query.Get();

this.listViewResult.BeginUpdate();
this.listViewResult.Clear();

foreach (ManagementObject queryData in queryCollection)
{
foreach (PropertyData queryDataRow in queryData.Properties)
{
try
{
ListViewItem listItem = new ListViewItem(queryDataRow.Name.ToString());
listItem.SubItems.Add(queryDataRow.Value.ToString());
this.listViewResult.Items.Add(listItem);
}
catch
{
continue;
}
}
}

this.listViewResult.Columns.Add("Name", 200, HorizontalAlignment.Left);
this.listViewResult.Columns.Add("Value", 200, HorizontalAlignment.Left);
this.listViewResult.EndUpdate();

Here is the result :


Aucun commentaire: