C # encapsulate and obtain the machine (CPU, hard disk, bios, and MAC address) Information

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.