什麼是WMI?
WMI就是Windows Management Instrumentation(Windows管理規范)。它是Windows中的一個核心管理技術。
.Net中怎樣使用WMI?
在.Net中,System.Management命名空間提供對系統管理信息和管理事件集合的訪問,這些信息和事件是與Windows管理規范(WMI)結構對系統,設備和應用程序設置檢測點有關的.一般情況下,應用程序和服務可以使用該命名空間下的ManagementObjectSearcher,ManagementScope,ManagementObject,ObjectQuery等類查詢興趣的管理信息(例如:在磁盤上還有多少剩余可用空間,當前CPU利用率,顯示設備信息等)WMI使用類似與SQL語句,甚至可以使用Where子句,更多WMI信息請參見:
http://www.microsoft.com/china/technet/community/scriptcenter/resources/wmifaq.mspx
下面將在C#用WMI查詢顯示卡信息:
1.新建一個Win應用程序
2.添加System.Management.dll引用,並引入該名稱空間
2.添加一個Button和ListVIEw,並設置相應屬性
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
namespace WMI信息
...{
public partial class Form1 : Form
...{
public Form1()
...{
InitializeComponent();
}
private void getInfomation_Click(object sender, EventArgs e)//getInfomation為Button
...{
try
...{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_VideoController");
foreach (ManagementObject info in searcher.Get())
...{
string[] item1 = ...{ "AdapterCompatibility", info["AdapterCompatibility"].ToString() };
listView1.Items.Add(new ListVIEwItem(item1));
string[] item2 = ...{ "AdapterDACType", info["AdapterDACType"].ToString() };
listView1.Items.Add(new ListVIEwItem(item2));
string[] item3 = ...{ "VideoModeDescription", info["VideoModeDescription"].ToString() };
listView1.Items.Add(new ListVIEwItem(item3));
string[] item4 = ...{ "Caption", info["Caption"].ToString() };
listView1.Items.Add(new ListVIEwItem(item4));
string[] item5 = ...{ "CurrentBitsPerPixel", info["CurrentBitsPerPixel"].ToString() };
listView1.Items.Add(new ListVIEwItem(item5));
string[] item6 =...{ "CurrentHorizontalResolution", info["CurrentHorizontalResolution"].ToString() };
listView1.Items.Add(new ListVIEwItem(item6));
string[] item7 = ...{ "VideoProcessor", info["VideoProcessor"].ToString() };
listView1.Items.Add(new ListVIEwItem(item7));
//還有很多屬性,在此不一一例舉,如:Status,PNPDeviceID,Name,MonoChrome,MinRefreshRate,MaxRefreshRate,InstalledDisplayDrivers
//InfSection,InfFilename,DriverVersion,DeviceSpecificPens,DeviceID,Description
}
}
catch
...{
//肯定會出現異常,但為了演示不處理異常!
}
}
}
}
利用WMI還可以查詢:
聲音設備信息:(SELECT * FROM Win32_SoundDevice)
驅動設備信息:(SELECT * FROM Win32_SystemDriver)
串口信息:(SELECT * FROM Win32_Serialport)
處理器信息:(SELECT * FROM Win32_Processor)
等等