C # obtain the CPU serial number, Nic MAC address, and hard disk serial number Encapsulation

Source: Internet
Author: User

 

/// <Summary> /// obtain the CPU and hard disk serial number of the computer, NIC address class // </Summary> class hardwareinfo {// <summary> // obtain the machine name /// </Summary> /// <returns> </ returns> Public String gethostname () {return system. net. DNS. gethostname () ;}/// <summary> // obtain the CPU serial number /// </Summary> /// <returns> </returns> Public static string getcpuserialnumber () {string cpuserialnumber = string. empty; using (managementclass MC = new managementclass ("win32_processor") {using (managementobjectcollection MOC = MC. getinstances () {foreach (managementobject Mo in MoC) {cpuserialnumber = Mo ["processorid"]. tostring (); break; }}return cpuserialnumber ;} /// <summary> /// obtain the hard disk serial number /// </Summary> /// <returns> </returns> Public static string getdiskserialnumber () {string diskserialnumber = string. empty; using (managementobjectsearcher MOS = new managementobjectsearcher () {MOS. query = new selectquery ("win32_diskdrive", "", new string [] {"pnpdeviceid", "signature"}); Using (managementobjectcollection mycollection = MOS. get () {using (managementobjectcollection. managementobjectenumerator em = mycollection. getenumerator () {em. movenext (); diskserialnumber = em. current. properties ["signature"]. value. tostring (). trim () ;}} return diskserialnumber ;} /// <summary> /// obtain the hardware address of the NIC // </Summary> /// <returns> </returns> Public static string getmoaddress () {string moaddress = ""; using (managementclass MC = new managementclass ("win32_networkadapterconfiguration") {using (managementobjectcollection moc2 = MC. getinstances () {foreach (managementobject Mo in moc2) {If (bool) Mo ["ipenabled"]) // determine whether the NIC is available {moaddress = Mo ["macaddress"]. tostring (); break ;}}} return moaddress ;}}

 

Call example:

String getcomputerinfo = hardwareinfo. getcpuserialnumber () + hardwareinfo. getdiskserialnumber () + hardwareinfo. getmoaddress ();

 

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.