簡體   English   中英

如何獲取網絡適配器的IP地址

[英]How to get IP address of network adapter

我使用以下代碼來獲取可用的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(=網絡適配器)? 我想這只是偶然。

謝謝羅伯特

您得到的答案並不完全正確,因為示例中的這四個IPv4地址都屬於網絡適配器,即使它們可能只是虛擬的也是如此。

要獲取有關您的網絡接口的更多信息,您應該檢查NetworkInterface類或WMI。 例如,您可以按類型過濾以刪除回送和隧道接口。

據我所知,實際使用哪個網絡適配器取決於要發送的數據包的目標地址。 網絡適​​配器使用地址解析協議來檢查它們是否可以到達目標IP和網關的MAC地址。

答案很短; 沒有默認的網絡適配器。

這是我使用的代碼:

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;
}

由於使用NetworkInterface的@Nappy Mentions是更好的方法。 以下是快速示例。

    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();
    }

嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM