首先需添加對system.management的引用。
view plaincopy to clipboardprint?
using system;
using system.runtime.interopservices;
using system.management;
namespace hardware
{
/// <summary>
/// hardwareinfo 的摘要說明。
/// </summary>
public class hardwareinfo
{
//取機器名
public string gethostname()
{
return system.net.dns.gethostname();
}
//取cpu編号
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
//取第一塊硬碟編号
public string getharddiskid()
managementobjectsearcher searcher = new managementobjectsearcher("select * from win32_physicalmedia");
string strharddiskid = null;
foreach (managementobject mo in searcher.get())
strharddiskid = mo["serialnumber"].tostring().trim();
return strharddiskid;
}//end
public enum ncbconst
ncbnamsz = 16, /* absolute length of a net name */
max_lana = 254, /* lana's in range 0 to max_lana inclusive */
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
[marshalas(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;
public struct name_buffer
[marshalas(unmanagedtype.byvalarray, sizeconst = (int)ncbconst.ncbnamsz)]
public byte[] name;
public byte name_num;
public byte name_flags;
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;
public byte[] ncb_callname;
public byte[] ncb_name;
public byte ncb_rto;
public byte ncb_sto;
public intptr ncb_post;
public byte ncb_lana_num;
public byte ncb_cmd_cplt;
[marshalas(unmanagedtype.byvalarray, sizeconst = 10)]
public byte[] ncb_reserve;
public intptr ncb_event;
public struct lana_enum
public byte length;
[marshalas(unmanagedtype.byvalarray, sizeconst = (int)ncbconst.max_lana)]
public byte[] lana;
[structlayout(layoutkind.auto)]
public struct astat
public adapter_status adapt;
[marshalas(unmanagedtype.byvalarray, sizeconst = (int)ncbconst.num_namebuf)]
public name_buffer[] namebuff;
public class win32api
[dllimport("netapi32.dll")]
public static extern char netbios(ref ncb ncb);
public string getmacaddress()
string addr = "";
int cb;
astat adapter;
ncb 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_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;
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("{0,2:x}{1,2:x}{2,2:x}{3,2:x}{4,2:x}{5,2: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]);
}
{ }
return addr.replace(' ', '0');
}
}
using system;
using system.runtime.interopservices;
using system.management;
namespace hardware
{
/// <summary>
/// hardwareinfo 的摘要說明。
/// </summary>
public class hardwareinfo
{
//取機器名
public string gethostname()
{
return system.net.dns.gethostname();
}
//取cpu編号
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
//取第一塊硬碟編号
public string getharddiskid()
managementobjectsearcher searcher = new managementobjectsearcher("select * from win32_physicalmedia");
string strharddiskid = null;
foreach (managementobject mo in searcher.get())
strharddiskid = mo["serialnumber"].tostring().trim();
return strharddiskid;
}//end
public enum ncbconst
ncbnamsz = 16, /* absolute length of a net name */
max_lana = 254, /* lana's in range 0 to max_lana inclusive */
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
[marshalas(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;
public struct name_buffer
[marshalas(unmanagedtype.byvalarray, sizeconst = (int)ncbconst.ncbnamsz)]
public byte[] name;
public byte name_num;
public byte name_flags;
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;
public byte[] ncb_callname;
public byte[] ncb_name;
public byte ncb_rto;
public byte ncb_sto;
public intptr ncb_post;
public byte ncb_lana_num;
public byte ncb_cmd_cplt;
[marshalas(unmanagedtype.byvalarray, sizeconst = 10)]
public byte[] ncb_reserve;
public intptr ncb_event;
public struct lana_enum
public byte length;
[marshalas(unmanagedtype.byvalarray, sizeconst = (int)ncbconst.max_lana)]
public byte[] lana;
[structlayout(layoutkind.auto)]
public struct astat
public adapter_status adapt;
[marshalas(unmanagedtype.byvalarray, sizeconst = (int)ncbconst.num_namebuf)]
public name_buffer[] namebuff;
public class win32api
[dllimport("netapi32.dll")]
public static extern char netbios(ref ncb ncb);
public string getmacaddress()
string addr = "";
int cb;
astat adapter;
ncb 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_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;
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("{0,2:x}{1,2:x}{2,2:x}{3,2:x}{4,2:x}{5,2: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]);
}
{ }
return addr.replace(' ', '0');
}
}
使用方法舉例:
//擷取硬碟序列号
hardware.hardwareinfo hardwareinfo = new hardware.hardwareinfo();
string harddiskid = hardwareinfo.getharddiskid();
system.console.writeline(harddiskid);
//擷取cpu序列号
string cpuid = hardwareinfo.getcpuid();
system.console.writeline(cpuid);
//擷取硬碟序列号hardware.hardwareinfo hardwareinfo = new hardware.hardwareinfo();string harddiskid = hardwareinfo.getharddiskid();system.console.writeline(harddiskid);//擷取cpu序列号string cpuid = hardwareinfo.getcpuid();system.console.writeline(cpuid);
本文轉載自csdn部落格,http://blog.csdn.net/songkexin/archive/2009/12/01/4916602.aspx