Using system;
Using system. runtime. interopservices;
Using system. management;
Namespace hardware
{
/// <Summary>
/// Summary of hardware_mac.
/// </Summary>
Public class hardwareinfo
{
// Obtain the machine name
Public string gethostname ()
{
Return system.net. dns. gethostname ();
}
// Obtain the cpu ID
Public string getcpuid ()
{
Try
{
Managementclass mc = new managementclass ("win32_processor ");
Managementobjectcollection moc = mc. getinstances ();
String strcpuid = null;
Foreach (managementobject mo in moc)
{
Strcpuid = mo. properties ["processorid"]. value. tostring ();
Break;
}
Return strcpuid;
}
Catch
{
Return "";
}
} // End method
// Obtain the cpu serial number www.qichepeijian.com
Public string getcpuid2 ()
{
Try
{
System. Management. ManagementObjectSearcher Wmi = new ManagementObjectSearcher ("SELECT * FROM Win32_Processor ");
String tmpUint32_1 = string. Empty;
Foreach (ManagementObject WmiObj in Wmi. Get ())
{
TmpUint32_1 = WmiObj ["ProcessorId"]. ToString ();
}
This. textBox2.Text = tmpUint32_1;
This. gProgressBar1.Value ++;
}
Catch (Exception ex2)
{
Throw new Exception (ex2.ToString ());
}
}
// Obtain the first hard disk number
Public string getharddiskid ()
{
Try
{
Managementobjectsearcher searcher = new managementobjectsearcher ("select * from win32_physicalmedia ");
String strharddiskid = null;
Foreach (managementobject mo in searcher. get ())
{
Strharddiskid = mo ["serialnumber"]. tostring (). trim ();
Break;
}
Return strharddiskid;
}
Catch
{
Return "";
}
} // End
// Obtain the disk serial number
Public string getharddiskid2 ()
{
Try
{
System. Management. ManagementObjectSearcher cmicWmi = new ManagementObjectSearcher ("SELECT * FROM Win32_DiskDrive ");
System. UInt32 tmpUint32 = 0;
Foreach (ManagementObject cmicWmiObj in cmicWmi. Get ())
{
TmpUint32 = Convert. ToUInt32 (cmicWmiObj ["signature"]. ToString ());
}
This. textBox1.Text = tmpUint32.ToString ();
This. gProgressBar1.Value ++;
}
Catch (Exception ex1)
{
Throw new Exception (ex1.ToString ());
}
}
/// <Summary>
/// Obtain the hard disk ID
/// </Summary>
/// <Returns> string </returns>
Public string GetHDid ()
{
String HDid = "";
Using (ManagementClass cimobject1 = new ManagementClass ("Win32_DiskDrive "))
{
ManagementObjectCollection moc1 = cimobject1.GetInstances ();
Foreach (ManagementObject mo in moc1)
{
HDid = (string) mo. Properties ["Model"]. Value;
Mo. Dispose ();
}
}
Return HDid. ToString ();
}
/// <Summary>
/// Obtain the hardware address of the NIC
/// </Summary>
/// <Returns> string </returns>
Public string GetMoAddress ()
{
String MoAddress = "";
Using (ManagementClass mc = new ManagementClass ("Win32_NetworkAdapterConfiguration "))
{
ManagementObjectCollection moc2 = mc. GetInstances ();
Foreach (ManagementObject mo in moc2)
{
If (bool) mo ["IPEnabled"] = true)
MoAddress = mo ["MacAddress"]. ToString ();
Mo. Dispose ();
}
}
Return MoAddress. ToString ();
}
}
}