C # obtain hardware information
// Pass the test
// System. management must be referenced.
Using system;
Using system. Management;
Namespace mycustomclasslib
{
/// <Summary>
/// Hardware information
/// </Summary>
Public class hardwareinfo
{
Public hardwareinfo ()
{
}
# Region hardware attributes
/// <Summary>
/// Machine name
/// </Summary>
Public String hostname
{
Get
{Return system. net. DNS. gethostname ();
}
}
/// <Summary>
/// CPU ID
/// </Summary>
Public String cpuid
{
Get {return getcpuid ();}
}
ASP. NET obtains the CPU serial number, hard disk serial number ID, and nic serial number.
Public static void getcpuinfo (out string cpuinfo)
{
// Obtain CPU Information
String _ cpuinfo = ""; // CPU Information
Managementclass cimobject = new managementclass ("win32_processor ");
Managementobjectcollection MOC = cimobject. getinstances ();
Foreach (managementobject Mo in MoC)
{
_ Cpuinfo = Mo. properties ["processorid"]. value. tostring ();
}
Cpuinfo = _ cpuinfo;
}
// Obtain the hard disk ID
String _ hdinfo = "";
Managementclass cimobject1 = new managementclass ("win32_diskdrive ");
Managementobjectcollection moc1 = cimobject1.getinstances ();
Foreach (managementobject Mo in moc1)
{
_ Hdinfo = (string) Mo. properties ["model"]. value;
}
Hdinfo = _ hdinfo;
}
Public static void getmacaddress (out string macaddress)
{
// Obtain the hardware address of the NIC
String _ macaddress = "";
Managementclass MC = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection moc2 = mc. getinstances ();
Foreach (managementobject Mo in moc2)
{
If (bool) Mo ["ipenabled"] = true)
Macaddress = Mo ["macaddress"]. tostring ();
Mo. Dispose ();
}
Macaddress = _ macaddress;
}
WMI Learning
After two days of study, we found that WMI is indeed a good thing. using it, we can easily manage computer hardware information, but the hardware information obtained with it is very limited, it is especially suitable for users who do not have high requirements on hardware information details. For example, some OEM vendors manage and detect online products, from then on, I have said Byebye to those tedious Apis ......
The following are some examples of my learning experience over the past two days:
// 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 serial number
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 ");
}
This article is from the csdn blog. For more information, see http://blog.csdn.net/wfy19850328/archive/2009/03/2379026030.aspx.