C # How to obtain the physical serial number of the cpu and Hard Disk

Source: Internet
Author: User

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 ();

}

}

}

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.