ibcadmin 发表于 2015-5-8 09:16:29

C#获取Ip地址和MAC地址

/// <summary>
/// 获取本地IP
/// </summary>
/// <returns></returns>
public string GetIP()
{
IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList;
return ipAddr.ToString();
}
/// <summary>
/// 获取mac地址
/// </summary>
/// <returns></returns>
public string getMacAddr_Local()
{
string madAddr = null;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc2 = mc.GetInstances();
foreach (ManagementObject mo in moc2)
{
if (Convert.ToBoolean(mo["IPEnabled"]) == true)
{
madAddr = mo["MacAddress"].ToString();
madAddr = madAddr.Replace(':', '-');
}
mo.Dispose();
}
return madAddr;
}

互联网小刚 发表于 2015-5-8 10:22:54

:lol

caozhao 发表于 2015-7-18 11:13:22

好, 去试试 谢谢分享

881966 发表于 2018-12-4 21:26:01

谢谢分享,学习学习
页: [1]
查看完整版本: C#获取Ip地址和MAC地址