比如说,有多个网卡,并且,分别配置的是IPV4或IPV6版本的IP地址,那么,我们如何通过C#代码获取C类的IP地址呢,比如192.168.0.123……
下面是代码:已测试成功!
ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = wmi.GetInstances();
foreach (ManagementObject mo in moc)
{
string tmp = mo["Description"].ToString();
if (tmp.Contains("PCI") && !tmp.Contains("Wireless") && (bool)mo["IPEnabled"] == true)
{
IPAddress[] arrIPAddresses = Dns.GetHostAddresses(Dns.GetHostName());
string[] ipaddress=(System.String[])mo["IPAddress"];
foreach (IPAddress ip in arrIPAddresses)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
Console.WriteLine(ip.ToString());
}
}
}
}