C # How to obtain the physical serial number of the hard disk

Source: Internet
Author: User
C # How to obtain the physical serial number of a hard disk

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 number
Public String getcpuid ()
{< br> try
{< br> managementclass MC = new managementclass ("win32_processor");
managementobjectcollection MOC = MC. getinstances ();
string strcpuid = NULL;
foreach (managementobject Mo in MoC)
{< br> strcpuid = Mo. properties ["processorid"]. value. tostring ();
break;
}< br> return strcpuid;
}< br> catch
{< br> return "";
}

} // End Method
 
// 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

Public Enum ncbconst
{
Ncbnamsz = 16,/* absolute length of a net name */
Max_lana = 254,/* Lana's in range 0 to max_lana portable sive */
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 rev_major;
Public byte reserved0;
Public byte adapter_type;
Public byte rev_minor;
Public ushort duration;
Public ushort frmr_recv;
Public ushort frmr_xmit;
Public ushort iframe_recv_err;
Public ushort xmit_aborts;
Public uint xmit_success;
Public uint recv_success;
Public ushort iframe_xmit_err;
Public ushort recv_buff_unavail;
Public ushort t1_timeouts;
Public ushort ti_timeouts;
Public uint reserved1;
Public ushort free_ncbs;
Public ushort max_cfg_ncbs;
Public ushort max_ncbs;
Public ushort xmit_buf_unavail;
Public ushort max_dgram_size;
Public ushort pending_sess;
Public ushort max_cfg_sess;
Public ushort max_sess;
Public ushort max_sess_pkt_size;
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 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
{< br> Public byte length;
[financialas (unmanagedtype. byvalarray, sizeconst = (INT) ncbconst. max_lana)]
Public byte [] Lana;
}

[structlayout (layoutkind. auto)]
public struct astat
{< br> Public adapter_status adapt;
[financialas (unmanagedtype. byvalarray, sizeconst = (INT) ncbconst. num_namebuf)]
Public name_buffer [] namebuff;
}< br> public class WIN32API
{< br> [dllimport ("netapi32.dll")]
Public static extern char NetBIOS (ref NCB);
}< br>
Public String getmacaddress ()
{< br> string ADDR = "";
try
{< br> 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 {< br> 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)
{< br> 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]);
}< BR >}< br> catch
{}< br> return ADDR. replace ('', '0');
}

}< BR >}< br>

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.