Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. interopservices;
Using system. Management;
Namespace myquery. utils
{
/// <Summary>
/// Encapsulate and obtain machine information
/// By Jia Shiyi 2011-4-26
/// </Summary>
Public static class machinehelper
{
/// <Summary>
/// Return volume serial number from hard drive
/// </Summary>
/// <Param name = "strdriveletter"> [Optional] drive letter </param>
/// <Returns> [String] volumeserialnumber </returns>
Public static string getvolumeserial (string strdriveletter)
{
If (strdriveletter = "" | strdriveletter = NULL) strdriveletter = "C ";
Managementobject disk =
New managementobject ("win32_logicaldisk.deviceid = \" "+ strdriveletter + ":\"");
Disk. Get ();
Return disk ["volumeserialnumber"]. tostring ();
}
/// <Summary>
/// Returns MAC address from first network card in Computer
/// </Summary>
/// <Returns> [String] MAC address </returns>
Public static string getmacaddress ()
{
Managementclass MC = new managementclass ("win32_networkadapterconfiguration ");
Managementobjectcollection MOC = mc. getinstances ();
String macaddress = string. empty;
Foreach (managementobject Mo in MoC)
{
If (macaddress = string. Empty) // only return MAC address from first card
{
If (bool) Mo ["ipenabled"] = true)
{
Macaddress = Mo ["macaddress"]. tostring ();
}
}
Else
{
Break;
}
Mo. Dispose ();
}
Macaddress = macaddress. Replace (":","");
Return macaddress;
}
/// <Summary>
/// Return processorid from first CPU in Machine
/// </Summary>
/// <Returns> [String] processorid </returns>
Public static string getcpuid ()
{
String cpuinfo = string. empty;
String temp = string. empty;
Managementclass MC = new managementclass ("win32_processor ");
Try
{
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
If (cpuinfo = string. Empty)
{// Only return cpuinfo from first CPU
Cpuinfo = Mo. properties ["processorid"]. value. tostring ();
}
Else
{
Break;
}
}
}
Catch
{
Cpuinfo = appdomain. currentdomain. basedirectory;
}
Finally
{
MC. Dispose ();
}
Return cpuinfo;
}
/// <Summary>
/// Return processorid from first CPU in Machine
/// </Summary>
/// <Returns> [String] processorid </returns>
Public static string getbiosnumber ()
{
Managementclass MC = new managementclass ("win32_bios ");
Managementobjectcollection MOC = mc. getinstances ();
String Sn = string. empty;
Foreach (managementobject Mo in MoC)
{
If (string. Empty. Equals (SN ))
{
Sn = Mo. properties ["serialnumber"]. value. tostring ();
}
Else
{
Break;
}
}
MC. Dispose ();
MOC. Dispose ();
Return Sn;
}
}
}
Welcome to: http: // 121.18.78.216, which is a demonstration platform for ease of query and analysis, workflow, content management, and project management.