C # obtain hardware information

Source: Internet
Author: User


1. Reference namespace

Using system. runtime. interopservices;
Using system. Management;

2. Obtain the machine name
Public String gethostname ()
{
Return System. net. DNS. gethostname ();
}

3. Obtain the CPU ID
Public String getcpuid ()
{
Try
{
Managementclass MC = new managementclass ("win32_processor ");
Managementobjectcollection MOC = mc. getinstances ()
String strcpuid = string. empty;
Foreach (managementobject Mo in MoC)
{
Strcpuid = Mo. properties ["processorid"]. value. tostring ();
Break;
}
Return strcpuid;
}
Catch (exception ER)
{
Return "";
}
}


4. Obtain the first hard disk number
Public String getharddiskid ()
{
Try
{
Managementobjectsearcher searcher = new managementobjectsearcher ("select * From win32_physicalmedia ");
String strharddiskid = string. empty;
Foreach (managementobject Mo in searcher. Get ())
{
Strharddiskid = Mo ["serialnumber"]. tostring (). Trim ();
Break;
}
Return strharddiskid;
}
Catch (exception ER)
{
// Handle exception
Return "";
}
}


5. Obtain the MAC address
[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 ncb_retcode;
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 ncb_rj_cplt;
[Financialas (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 );
}

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;
Uretcode = 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 (exception ex)
{
// Handle exception
}
Return ADDR. Replace ('', '0 ');
}

C # obtain hardware information

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.