using System.Runtime.InteropServices;
using System.Management;
2. 取机器名
public string GetHostName()
{
return System.Net.Dns.GetHostName();
}
3. 取CPU编号
public String GetCpuID()
{
try
{
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances()
String strCpuID = string.Empty;
foreach (ManagementObject mo in moc)
{
strCpuID = mo.Properties["ProcessorId"].Value.ToString();
break;
}
return strCpuID;
}
catch(Exception er)
{
return "";
}
}
4. 取第一块硬盘编号
public String GetHardDiskID()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
String strHardDiskID = string.Empty;
foreach (ManagementObject mo in searcher.Get())
{
strHardDiskID = mo["SerialNumber"].ToString().Trim();
break;
}
return strHardDiskID;
}
catch(Exception er)
{
//handle exception
return "";
}
}
5. 以下是取得Mac地址
[StructLayout(LayoutKind.Sequential)]
public struct NAME_BUFFER
{
[MarshalAs(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;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]
public byte[] ncb_callname;
[MarshalAs(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_cmd_cplt;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public byte[] ncb_reserve;
public IntPtr ncb_event;
}
[StructLayout(LayoutKind.Sequential)]
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 = "";
try
{
int cb;
ASTAT adapter;
NCB Ncb = new NCB();
char uRetCode;
LANA_ENUM lenum;