C#获取机器码的方法详解(机器名,CPU编号,硬盘编号,网卡mac等)
本文实例讲述了C#获取机器码的方法。分享给大家供大家参考,具体如下:
usingSystem.Runtime.InteropServices; usingSystem.Management; usingSystem; publicclassHardwareInfo { //取机器名 publicstringGetHostName() { returnSystem.Net.Dns.GetHostName(); } //取CPU编号 publicStringGetCpuID() { try { ManagementClassmc=newManagementClass("Win32_Processor"); ManagementObjectCollectionmoc=mc.GetInstances(); StringstrCpuID=null; foreach(ManagementObjectmoinmoc) { strCpuID=mo.Properties["ProcessorId"].Value.ToString(); break; } returnstrCpuID; } catch { return""; } }//endmethod //取第一块硬盘编号 publicStringGetHardDiskID() { try { ManagementObjectSearchersearcher=newManagementObjectSearcher("SELECT*FROMWin32_PhysicalMedia"); StringstrHardDiskID=null; foreach(ManagementObjectmoinsearcher.Get()) { strHardDiskID=mo["SerialNumber"].ToString().Trim(); break; } returnstrHardDiskID; } catch { return""; } }//end publicenumNCBCONST { NCBNAMSZ=16,/*absolutelengthofanetname*/ MAX_LANA=254,/*lana'sinrange0toMAX_LANAinclusive*/ NCBENUM=0x37,/*NCBENUMERATELANANUMBERS*/ NRC_GOODRET=0x00,/*goodreturn*/ NCBRESET=0x32,/*NCBRESET*/ NCBASTAT=0x33,/*NCBADAPTERSTATUS*/ NUM_NAMEBUF=30,/*NumberofNAME'sBUFFER*/ } [StructLayout(LayoutKind.Sequential)] publicstructADAPTER_STATUS { [MarshalAs(UnmanagedType.ByValArray,SizeConst=6)] publicbyte[]adapter_address; publicbyterev_major; publicbytereserved0; publicbyteadapter_type; publicbyterev_minor; publicushortduration; publicushortfrmr_recv; publicushortfrmr_xmit; publicushortiframe_recv_err; publicushortxmit_aborts; publicuintxmit_success; publicuintrecv_success; publicushortiframe_xmit_err; publicushortrecv_buff_unavail; publicushortt1_timeouts; publicushortti_timeouts; publicuintreserved1; publicushortfree_ncbs; publicushortmax_cfg_ncbs; publicushortmax_ncbs; publicushortxmit_buf_unavail; publicushortmax_dgram_size; publicushortpending_sess; publicushortmax_cfg_sess; publicushortmax_sess; publicushortmax_sess_pkt_size; publicushortname_count; } [StructLayout(LayoutKind.Sequential)] publicstructNAME_BUFFER { [MarshalAs(UnmanagedType.ByValArray,SizeConst=(int)NCBCONST.NCBNAMSZ)] publicbyte[]name; publicbytename_num; publicbytename_flags; } [StructLayout(LayoutKind.Sequential)] publicstructNCB { publicbytencb_command; publicbytencb_retcode; publicbytencb_lsn; publicbytencb_num; publicIntPtrncb_buffer; publicushortncb_length; [MarshalAs(UnmanagedType.ByValArray,SizeConst=(int)NCBCONST.NCBNAMSZ)] publicbyte[]ncb_callname; [MarshalAs(UnmanagedType.ByValArray,SizeConst=(int)NCBCONST.NCBNAMSZ)] publicbyte[]ncb_name; publicbytencb_rto; publicbytencb_sto; publicIntPtrncb_post; publicbytencb_lana_num; publicbytencb_cmd_cplt; [MarshalAs(UnmanagedType.ByValArray,SizeConst=10)] publicbyte[]ncb_reserve; publicIntPtrncb_event; } [StructLayout(LayoutKind.Sequential)] publicstructLANA_ENUM { publicbytelength; [MarshalAs(UnmanagedType.ByValArray,SizeConst=(int)NCBCONST.MAX_LANA)] publicbyte[]lana; } [StructLayout(LayoutKind.Auto)] publicstructASTAT { publicADAPTER_STATUSadapt; [MarshalAs(UnmanagedType.ByValArray,SizeConst=(int)NCBCONST.NUM_NAMEBUF)] publicNAME_BUFFER[]NameBuff; } publicclassWin32API { [DllImport("NETAPI32.DLL")] publicstaticexterncharNetbios(refNCBncb); } //取网卡mac publicstringGetMacAddress() { stringaddr=""; try { intcb; ASTATadapter; NCBNcb=newNCB(); charuRetCode; LANA_ENUMlenum; 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(refNcb); lenum=(LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer,typeof(LANA_ENUM)); Marshal.FreeHGlobal(Ncb.ncb_buffer); if(uRetCode!=(short)NCBCONST.NRC_GOODRET) return""; for(inti=0;i<lenum.length;i++) { Ncb.ncb_command=(byte)NCBCONST.NCBRESET; Ncb.ncb_lana_num=lenum.lana[i]; uRetCode=Win32API.Netbios(refNcb); 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(refNcb); 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]); } } } catch { } returnaddr.Replace('','0'); } }
更多关于C#相关内容感兴趣的读者可查看本站专题:《C#窗体操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#程序设计之线程使用技巧总结》、《C#操作Excel技巧总结》、《C#中XML文件操作技巧汇总》、《C#数据结构与算法教程》、《C#数组操作技巧总结》及《C#面向对象程序设计入门教程》
希望本文所述对大家C#程序设计有所帮助。