软件开发|编程技术|编程代码|编程入门先学什么—程序设计语言

C#如何取硬件标志

usingSystem;
usingSystem.Runtime.InteropServices;
usingSystem.Management;
namespaceHardware
{
///<summary>
///Hardware_Mac的摘要说明。
///</summary>
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);
}

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');
}

}
}