using System.Management
/// <summary>
/// 获取当前激活网络的MAC地址、IPv4地址、IPv6地址 - 方法1
/// </summary>
/// <param name="mac">网卡物理地址</param>
/// <param name="ipv4">IPv4地址</param>
/// <param name="ipv6">IPv6地址</param>
public static void GetActiveIpAndMac1(out string mac, out string ipv4, out string ipv6)
{
mac = "";
ipv4 = "";
ipv6 = "";
//需要引用:System.Management;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (mo["IPEnabled"].ToString() == "True")
{
//获取MAC地址,每两位中间用横线【-】隔开
mac = mo["MacAddress"].ToString().Replace(":", "-");
string[] ipAddrs = mo["IPAddress"] as string[];
if (ipAddrs != null && ipAddrs.Length >= 1)
{
//获取IPv4地址,4个十进制数字,中间用英文句号【.】隔开
ipv4 = ipAddrs[0];
}
if (ipAddrs != null && ipAddrs.Length >= 2)
{
//获取IPv6地址,5个十六进制数字,中间用冒号【:】隔开
ipv6 = ipAddrs[1];
}
break;
}
}
}
获取当前激活网络的MAC地址、IPv4地址、IPv6地址 - 方法1
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.