学习WMI有感

通过两天的学习发现WMI确实是个好东西:),利用它我们可以很方便的对计算机的硬件信息进行管理,但是用它得到的硬件信息十分有限,特别适用于对硬件信息细节要求不高的用户,例如一些OEM厂商对产线上的产品进行管理和检测,有了它,从此对那些繁琐的API说声byebye了......

下面这几个小例子是我这两天的学习心得:

//Get LAN MAC address
static void GetLanMACAddress()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
Console.WriteLine("MAC address/t{0}", mo["MacAddress"].ToString());
mo.Dispose();
}
Console.WriteLine ("/n/r");
}
//Get HDD Size / Serial # / Firmware
static void GetHDDInfo()
{
String HDid;
ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
HDid = (string)mo.Properties["Model"].Value;

//Console.WriteLine("HDD Serial/t{0}", mo["Serial Number"].ToString());

Console.WriteLine("HDD Size/t{0} Bytes", mo["Size"].ToString());
Console.WriteLine("HDD Model/t{0}", mo["Model"].ToString());
Console.WriteLine("HDD Manufacturer /t{0}", mo["Manufacturer"].ToString());
//Console.WriteLine("HDD Model/t{0}", mo["Firmware"].ToString());

mo.Dispose ();
}

Console.WriteLine ("/n/r");
}
//Get CPU INFO.
static void GetCpuInfo()
{
//string cpuInfo = "";//cpu序列号
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
//cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
Console.WriteLine("ProcessorId:/t{0}", mo["ProcessorId"].ToString());
Console.WriteLine("CurrentClockSpeed:/t{0}", mo["CurrentClockSpeed"].ToString());

//Console.WriteLine("L2CacheSize:/t{0}", mo["L2CacheSize"].ToString());
//Console.WriteLine("L2CacheSpeed:/t{0}", mo["L2CacheSpeed"].ToString());

Console.WriteLine("Description:/t{0}", mo["Description"].ToString());
Console.WriteLine("ProcessorType:/t{0}", mo["ProcessorType"].ToString());
Console.WriteLine("Version:/t{0}", mo["Version"].ToString());
Console.WriteLine("Revision:/t{0}", mo["Revision"].ToString());
Console.WriteLine("Manufacturer:/t{0}", mo["Manufacturer"].ToString());
//Console.WriteLine(cpuInfo);
}
Console.WriteLine ("/n/r");
}
//Get Cahce INFO.
static void GetCacheInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_CacheMemory");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("MaxCacheSize:/t{0}", mo["MaxCacheSize"].ToString());
Console.WriteLine("Location:/t{0}", mo["Purpose"].ToString());
}
Console.WriteLine ("/n/r");
}
//Get Memory INFO.
static void GetMemoryInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_PhysicalMemory");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("Capacity:/t{0}", mo["Capacity"].ToString());
Console.WriteLine("DeviceLocator:/t{0}", mo["DeviceLocator"].ToString());
Console.WriteLine("Description:/t{0}", mo["Description"].ToString());
}
Console.WriteLine ("/n/r");
}
//Get the battery INFO.
static void GetBatteryInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_Battery");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
//Console.WriteLine("Capacity:/t{0}", mo["BatteryRechargeTime"].ToString());
//Console.WriteLine("DeviceLocator:/t{0}", mo["BatteryStatus"].ToString());
Console.WriteLine("Description:/t{0}", mo["Chemistry"].ToString());
}
Console.WriteLine ("/n/r");
}
//Get the LCD INFO.
static void GetLCDInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_DesktopMonitor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("Description:/t{0}", mo["Description"].ToString());
Console.WriteLine("PixelsPerXLogicalInch:/t{0}", mo["PixelsPerXLogicalInch"].ToString());
Console.WriteLine("PixelsPerYLogicalInch:/t{0}", mo["PixelsPerYLogicalInch"].ToString());
Console.WriteLine("Manufacturer:/t{0}", mo["MonitorManufacturer"].ToString ()); //[]
Console.WriteLine("MonitorType:/t{0}", mo["MonitorType"].ToString());
Console.WriteLine("ScreenHeight:/t{0}", mo["ScreenHeight"].ToString());
Console.WriteLine("ScreenWidth:/t{0}", mo["ScreenWidth"].ToString ());//datetime to string
}
Console.WriteLine ("/n/r");
}
//Get the BIOS INFO.
static void GetBIOSInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_BIOS");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("Version:/t{0}", mo["Version"].ToString());
//Console.WriteLine("DeviceLocator:/t{0}", mo["BuildNumber"].ToString());
Console.WriteLine("BIOSVersion:/t{0}", mo["BIOSVersion"].ToString ()); //[]
Console.WriteLine("Manufacturer:/t{0}", mo["Manufacturer"].ToString());
Console.WriteLine("SerialNumber:/t{0}", mo["SerialNumber"].ToString());
//Console.WriteLine("InstallDate:/t{0}", (mo["InstallDate"].ToString ());//datetime to string
}
Console.WriteLine ("/n/r");
}
//Get HDD SerialNum
public static void GetHd()
{
ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher();

wmiSearcher.Query = new SelectQuery(
"Win32_DiskDrive",
"",
new string[]{"PNPDeviceID"}
);
ManagementObjectCollection myCollection = wmiSearcher.Get();
ManagementObjectCollection.ManagementObjectEnumerator em =
myCollection.GetEnumerator();
em.MoveNext();
ManagementBaseObject mo = em.Current;
string id = mo.Properties["PNPDeviceID"].Value.ToString().Trim();
Console.WriteLine("My harddisk is : " + id);
}
//Get Monitor INFO.
public static void GetMonitorInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_DisplayConfiguration");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("Description:/t{0}", mo["Description"].ToString());
Console.WriteLine("DisplayFrequency:/t{0}", mo["DisplayFrequency"].ToString());
Console.WriteLine("PelsHeight:/t{0}", mo["PelsHeight"].ToString());
Console.WriteLine("PelsWidth:/t{0}", mo["PelsWidth"].ToString ()); //[]
//Console.WriteLine("MonitorType:/t{0}", mo["MonitorType"].ToString());
//Console.WriteLine("ScreenHeight:/t{0}", mo["ScreenHeight"].ToString());
//Console.WriteLine("ScreenWidth:/t{0}", mo["ScreenWidth"].ToString ());//datetime to string
}
Console.WriteLine ("/n/r");

}

阅读更多
文章标签: string api 产品
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭