第一种 public class OperateMAC { public static string GetMacByWMI() { string MacAddr = null; //ManagementObjectSearcher Query = new ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration"); //ManagementObjectCollection
第一种
public class OperateMAC
{
public static string GetMacByWMI()
{
string MacAddr = null;
//ManagementObjectSearcher Query = new ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration");
//ManagementObjectCollection QueryCollection = Query.Get();
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (mo["IPEnabled"].ToString() == "True")
MacAddr = mo["MacAddress"].ToString();
}
return MacAddr;
} public static string GetIpByWMI()
{
string IpAddr = string.Empty;
System.NET.IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
for (int i = 0; i < addressList.Length; i++)
{
IpAddr = addressList[i].ToString();
}
return IpAddr;
}
}
第二种
private string GetMacAddress()
{
List<string> macs = new List<string>();
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
macs.Add(ni.GetPhysicalAddress().ToString());
}
return macs.FirstOrDefault();
}
private string GetIPAddress()
{
string strHostName = Dns.GetHostName(); //得到本机的主机名
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); //取得本机IP
if (ipEntry.AddressList.Length == 0) return null;
IPAddress ipAddress = ipEntry.AddressList.FirstOrDefault(item => item.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
if (ipAddress == null) ipAddress = ipEntry.AddressList[0];
return ipAddress.ToString();
}
龙腾一族至尊龙骑