WMI必須引用System.Management命名空間
WMI的兩種查詢方式
WQL方式
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_OperatingSystem");
foreach (ManagementObject mo in mos.Get())
{
txtProductID.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() =>
{
ProductID.Text = (string)mo["SerialNumber"];
}));
}
數據對象方式
ManagementClass cimobject2 = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc2 = cimobject2.GetInstances();
foreach (ManagementObject mo1 in moc2)
{
txtSystemType.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() =>
{
SystemType.Text = mo1.Properties["SystemType"].Value.ToString();
}));
}
兩種方式大同小異
查看不同的對象所包含的內容,使用 運行→wbemtest,通過WQL語句進行查詢。