首先需添加对Syst
using System;using System.Run time.Inter opServices ;using System.Man agement;na mespace Hardware{ /// <summary> /// HardwareIn fo 的摘要说明。 /// </summary> public class HardwareIn fo { //取机器名 public string GetHostNam e() { return System.Net .Dns.GetHo stName(); } //取CPU编号 public String GetCpuID() { try { Management Class mc = new Management Class("Win 32_Process or"); Management ObjectColl ection moc = mc.GetInst ances(); String strCpuID = null; foreach (Managemen tObject mo in moc) { strCpuID = mo.Propert ies["Proce ssorId"].V alue.ToStr ing(); break; } return strCpuID; } catch { return ""; } }//end method //取第一块硬盘编号 public String GetHardDis kID() { try { Management ObjectSear cher searcher = new Management ObjectSear cher("SELE CT * FROM Win32_Phys icalMedia" ); String strHardDis kID = null; foreach (Managemen tObject mo in searcher.G et()) { strHardDis kID = mo["Serial Number"].T oString(). Trim(); break; } return strHardDis kID; } 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 inclusive */ NCBENUM = 0x37, /* NCB ENUMERATE LANA NUMBERS */ NRC_GOODRE T = 0x00, /* good return */ NCBRESET = 0x32, /* NCB RESET */ NCBASTAT = 0x33, /* NCB ADAPTER STATUS */ NUM_NAMEBU F = 30, /* Number of NAME's BUFFER */ } [StructLay out(Layout Kind.Seque ntial)] public struct ADAPTER_ST ATUS { [MarshalAs (Unmanaged Type.ByVal Array, SizeConst = 6)] public byte[] adapter_ad dress; public byte rev_major; public byte reserved0; public byte adapter_ty pe; public byte rev_minor; public ushort duration; public ushort frmr_recv; public ushort frmr_xmit; public ushort iframe_rec v_err; public ushort xmit_abort s; public uint xmit_succe ss; public uint recv_succe ss; public ushort iframe_xmi t_err; public ushort recv_buff_ unavail; public ushort t1_timeout s; public ushort ti_timeout s; public uint reserved1; public ushort free_ncbs; public ushort max_cfg_nc bs; public ushort max_ncbs; public ushort xmit_buf_u navail; public ushort max_dgram_ size; public ushort pending_se ss; public ushort max_cfg_se ss; public ushort max_sess; public ushort max_sess_p kt_size; public ushort name_count ; } [StructLay out(Layout Kind.Seque ntial)] public struct NAME_BUFFE R { [MarshalAs (Unmanaged Type.ByVal Array, SizeConst = (int)NCBCO NST.NCBNAM SZ)] public byte[] name; public byte name_num; public byte name_flags ; } [StructLay out(Layout Kind.Seque ntial)] public struct NCB { public byte ncb_comman d; public byte ncb_retcod e; public byte ncb_lsn; public byte ncb_num; public IntPtr ncb_buffer ; public ushort ncb_length ; [MarshalAs (Unmanaged Type.ByVal Array, SizeConst = (int)NCBCO NST.NCBNAM SZ)] public byte[] ncb_callna me; [MarshalAs (Unmanaged Type.ByVal Array, SizeConst = (int)NCBCO NST.NCBNAM SZ)] public byte[] ncb_name; public byte ncb_rto; public byte ncb_sto; public IntPtr ncb_post; public byte ncb_lana_n um; public byte ncb_cmd_cp lt; [MarshalAs (Unmanaged Type.ByVal Array, SizeConst = 10)] public byte[] ncb_reserv e; public IntPtr ncb_event; } [StructLay out(Layout Kind.Seque ntial)] public struct LANA_ENUM { public byte length; [MarshalAs (Unmanaged Type.ByVal Array, SizeConst = (int)NCBCO NST.MAX_LA NA)] public byte[] lana; } [StructLay out(Layout Kind.Auto) ] public struct ASTAT { public ADAPTER_ST ATUS adapt; [MarshalAs (Unmanaged Type.ByVal Array, SizeConst = (int)NCBCO NST.NUM_NA MEBUF)] public NAME_BUFFE R[] NameBuff; } public class Win32API { [DllImport ("NETAPI32 .DLL")] public static extern char Netbios(re f NCB ncb); } public string GetMacAddr ess() { string addr = ""; try { int cb; ASTAT adapter; NCB Ncb = new NCB(); char uRetCode; LANA_ENUM lenum; Ncb.ncb_co mmand = (byte)NCBC ONST.NCBEN UM; cb = Marshal.Si zeOf(typeo f(LANA_ENU M)); Ncb.ncb_bu ffer = Marshal.Al locHGlobal (cb); Ncb.ncb_le ngth = (ushort)cb ; uRetCode = Win32API.N etbios(ref Ncb); lenum = (LANA_ENUM )Marshal.P trToStruct ure(Ncb.nc b_buffer, typeof(LAN A_ENUM)); Marshal.Fr eeHGlobal( Ncb.ncb_bu ffer); if (uRetCode != (short)NCB CONST.NRC_ GOODRET) return ""; for (int i = 0; i < lenum.leng th; i++) { Ncb.ncb_co mmand = (byte)NCBC ONST.NCBRE SET; Ncb.ncb_la na_num = lenum.lana [i]; uRetCode = Win32API.N etbios(ref Ncb); if (uRetCode != (short)NCB CONST.NRC_ GOODRET) return ""; Ncb.ncb_co mmand = (byte)NCBC ONST.NCBAS TAT; Ncb.ncb_la na_num = lenum.lana [i]; Ncb.ncb_ca llname[0] = (byte)'*'; cb = Marshal.Si zeOf(typeo f(ADAPTER_ STATUS)) + Marshal.Si zeOf(typeo f(NAME_BUF FER)) * (int)NCBCO NST.NUM_NA MEBUF; Ncb.ncb_bu ffer = Marshal.Al locHGlobal (cb); Ncb.ncb_le ngth = (ushort)cb ; uRetCode = Win32API.N etbios(ref Ncb); adapter.ad apt = (ADAPTER_S TATUS)Mars hal.PtrToS tructure(N cb.ncb_buf fer, typeof(ADA PTER_STATU S)); Marshal.Fr eeHGlobal( Ncb.ncb_bu ffer); if (uRetCode == (short)NCB CONST.NRC_ GOODRET) { if (i > 0) addr += ":"; addr = string.For mat("{0,2: X}{1,2:X}{ 2,2:X}{3,2 :X}{4,2:X} {5,2:X}", adapter.ad apt.adapte r_address[ 0], adapter.ad apt.adapte r_address[ 1], adapter.ad apt.adapte r_address[ 2], adapter.ad apt.adapte r_address[ 3], adapter.ad apt.adapte r_address[ 4], adapter.ad apt.adapte r_address[ 5]); } } } catch { } return addr.Repla ce(' ', '0'); } }}
使用方法举例:
联系客服