C # obtain the hard disk and cpu id and nic Mac

Source: Internet
Author: User
/// <Summary> /// obtain the hard disk and cpu id and nic Mac /// </Summary> public class hardwareinfo {// <summary> // obtain the machine name /// </Summary> /// <returns> </returns> Public String gethostname () {return system. net. DNS. gethostname () ;}/// <summary> /// obtain the cpu id /// </Summary> /// <returns> </returns> 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 /// <summary> /// obtain the first hard disk number /// </Summary> /// <returns> </returns> Public String getharddiskid () {try {// managementobjectsearcher searcher = new managementobjectsearcher ("select * From win32_physi Calmedia "); string strharddiskid = NULL; managementclass cimobject = new managementclass (" win32_diskdrive "); managementobjectcollection MOC = cimobject. getinstances (); foreach (managementobject Mo in MoC) {strharddiskid = (string) Mo. properties ["model"]. value; break;} // foreach (managementobject Mo in searcher. get () // {// strharddiskid = Mo ["serialnumber"]. tostring (). trim (); // break; //} retur N strharddiskid;} catch {return "";}} /// <summary> /// obtain the NIC Mac /// </Summary> /// <returns> </returns> Public String getmacaddress () {string ADDR = ""; try {int CB; astat adapter; NCB = new NCB (); char uretcode; lana_enum lenum; NCB. ncb_command = (byte) ncbconst. ncbenum; Cb = marshal. sizeof (typeof (lana_enum); NCB. ncb_buffer = marshal. allochglobal (CB); NCB. ncb_length = (ushort) Cb; uretcod E = WIN32API. netBIOS (ref NCB); lenum = (lana_enum) Marshal. ptrtostructure (NCB. ncb_buffer, typeof (lana_enum); marshal. freehglobal (NCB. ncb_buffer); If (uretcode! = (Short) ncbconst. nrc_goodret) Return ""; for (INT I = 0; I <lenum. length; I ++) {NCB. ncb_command = (byte) ncbconst. ncbreset; NCB. ncb_lana_num = lenum. lana [I]; uretcode = WIN32API. netBIOS (ref NCB); If (uretcode! = (Short) ncbconst. nrc_goodret) Return ""; NCB. ncb_command = (byte) ncbconst. ncbastat; NCB. ncb_lana_num = lenum. lana [I]; NCB. ncb_callname [0] = (byte) '*'; Cb = marshal. sizeof (typeof (adapter_status) + marshal. sizeof (typeof (name_buffer) * (INT) ncbconst. num_namebuf; NCB. ncb_buffer = marshal. allochglobal (CB); NCB. ncb_length = (ushort) Cb; uretcode = WIN32API. netBIOS (ref NCB); adapter. adapt = (adapter_status) Marshal. ptrtostructure (NCB. ncb_buffer, typeof (adapter_status); marshal. freehglobal (NCB. ncb_buffer); If (uretcode = (short) ncbconst. nrc_goodret) {if (I> 0) ADDR + = ":"; ADDR = string. format ("{: x}", adapter. adapt. adapter_address [0], adapter. adapt. adapter_address [1], adapter. adapt. adapter_address [2], adapter. adapt. adapter_address [3], adapter. adapt. adapter_address [4], adapter. adapt. adapter_address [5]) ;}} catch {} return ADDR. replace ('', '0');} public Enum ncbconst {ncbnamsz = 16,/* absolute length of a net name */max_lana = 254, /* Lana's in range 0 to max_lana intrusive */ncbenum = 0x37,/* NCB enumerate Lana numbers */nrc_goodret = 0x00, /* good return */ncbreset = 0x32,/* NCB reset */ncbastat = 0x33,/* NCB adapter status */num_namebuf = 30, /* Number of name's buffer */} [structlayout (layoutkind. sequential)] public struct adapter_status {[financialas (unmanagedtype. byvalarray, sizeconst = 6)] public byte [] adapter_address; Public byte region; Public byte reserved0; Public byte adapter_type; Public byte rev_minor; Public ushort duration; Public ushort frmr_recv; public ushort instances; Public uint instances; Public ushort t1_timeouts; Public ushort ti_timeouts; Public uint reserved1; public ushort free_ncbs; Public ushort protocol; Public ushort max_ncbs; Public ushort protocol; Public ushort pending_sess; Public ushort protocol; Public ushort max_sess; Public ushort protocol; public ushort name_count;} [structlayout (layoutkind. sequential)] public struct name_buffer {[financialas (unmanagedtype. byvalarray, sizeconst = (INT) ncbconst. ncbnamsz)] public byte [] Name; Public byte name_num; Public byte name_flags;} [structlayout (layoutkind. sequential)] public struct NCB {public byte ncb_command; Public byte serial; Public byte ncb_lsn; Public byte ncb_num; Public intptr ncb_buffer; Public ushort ncb_length; [financialas (unmanagedtype. byvalarray, sizeconst = (INT) ncbconst. ncbnamsz)] public byte [] ncb_callname; [financialas (unmanagedtype. byvalarray, sizeconst = (INT) ncbconst. ncbnamsz)] public byte [] ncb_name; Public byte ncb_rto; Public byte ncb_sto; Public intptr ncb_post; Public byte ncb_lana_num; Public byte loss; [same Alas (unmanagedtype. byvalarray, sizeconst = 10)] public byte [] ncb_reserve; Public intptr ncb_event;} [structlayout (layoutkind. sequential)] public struct lana_enum {public byte length; [financialas (unmanagedtype. byvalarray, sizeconst = (INT) ncbconst. max_lana)] public byte [] Lana;} [structlayout (layoutkind. auto)] public struct astat {public adapter_status adapt; [financialas (unmanagedtype. byvalarray, sizeconst = (INT) ncbconst. num_namebuf)] public name_buffer [] namebuff;} public class WIN32API {[dllimport ("netapi32.dll")] public static extern char NetBIOS (ref NCB );}}

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.