如何获取网络适配器的IP地址

时间:2012-01-18 21:55:32

标签: c# windows ip-address

我使用此代码获取可用的IPv4地址:

static void Main(string[] args)
    {
        string host = System.Net.Dns.GetHostName();
        System.Net.IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(host);
        System.Net.IPAddress[] ipAddr = ipEntry.AddressList;
        for (int i = 0; i < ipAddr.Length; i++)
        {
            if (ipAddr[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                Console.WriteLine( ipAddr[i]);

        }
    }

对于我的机器,目前提供:

192.168.1.11

192.168.240.1

192.168.182.1

10.1.1.121

而192.168.1.11是我的网络适配器,接下来的两个来自VMware Network,10.1.1.121来自当前活动的OpenVPN连接。

如何才能可靠地检测IPv4地址192.168.1.11(=网络适配器)? 我想这只是偶然的第一个地方。

谢谢,罗伯特

4 个答案:

答案 0 :(得分:8)

您得到的答案并非完全正确,因为您示例中的这四个IPv4地址都属于网络适配器,即使它们可能只是虚拟的。

要获取有关网络接口的更多信息,请检查NetworkInterface Class或WMI。您可以按类型过滤掉以删除环回和隧道接口,例如。

据我所知,实际使用的网络适配器取决于您要发送的数据包的目标地址。网络适​​配器使用地址解析协议来检查它们是否可以到达desination IP和网关的MAC地址。

如此简短的回答;没有默认的网络适配器。

答案 1 :(得分:2)

这是我使用的代码:

private string getLocalIP()
{
    string Localip = "?";
    foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
    {

        var defaultGateway =  from nics in NetworkInterface.GetAllNetworkInterfaces()


from props in nics.GetIPProperties().GatewayAddresses
   where nics.OperationalStatus == OperationalStatus.Up
   select props.Address.ToString(); // this sets the default gateway in a variable

        GatewayIPAddressInformationCollection prop = netInterface.GetIPProperties().GatewayAddresses;

        if(defaultGateway.First() != null){

        IPInterfaceProperties ipProps = netInterface.GetIPProperties();

        foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses)
        {

            if (addr.Address.ToString().Contains(defaultGateway.First().Remove(defaultGateway.First().LastIndexOf(".")))) // The IP address of the computer is always a bit equal to the default gateway except for the last group of numbers. This splits it and checks if the ip without the last group matches the default gateway
            {

                if (Localip == "?") // check if the string has been changed before
                {
                    Localip = addr.Address.ToString(); // put the ip address in a string that you can use.
                }
            }

        }

        }

    }
   return Localip;
}

答案 2 :(得分:0)

@Nappy提到使用NetworkInterface是一种更好的方法。下面的快速示例。

    private IEnumerable<IPAddress> GetIpsForNetworkAdapters()
    {

        var nics = from i in NetworkInterface.GetAllNetworkInterfaces()
                    where i.OperationalStatus == OperationalStatus.Up
                    select new { name = i.Name, ip = GetIpFromUnicastAddresses(i) };

        return nics.Select(x => x.ip);
    }

    private IPAddress GetIpFromUnicastAddresses(NetworkInterface i)
    {
        return (from ip in i.GetIPProperties().UnicastAddresses
                where ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork
                select ip.Address).SingleOrDefault();
    }

答案 3 :(得分:-1)

试试这个

System.Net.Dns.GetHostByName(Environment.MachineName).AddressList[0].ToString();