簡體   English   中英

如何獲取本地機器的IPv4和IPv6地址?

[英]How to get IPv4 and IPv6 address of local machine?

我正在開發一個Windows應用程序,我需要找到本地機器的IPv4和IPv6地址。 操作系統可以是XP或Windows 7。

我得到了一個獲取MAC地址的解決方案,

string GetMACAddress()
{
    var macAddr =
        (
            from nic in NetworkInterface.GetAllNetworkInterfaces()
            where nic.OperationalStatus == OperationalStatus.Up
            select nic.GetPhysicalAddress().ToString()
        ).FirstOrDefault();

    return macAddr.ToString();
}

這適用於所有操作系統。

獲取適用於XP和WINDOWS 7的IPv4和IPv6地址的正確方法是什么?

string strHostName = System.Net.Dns.GetHostName();;
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
Console.WriteLine(addr[addr.Length-1].ToString());
if (addr[0].AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
            {
                Console.WriteLine(addr[0].ToString()); //ipv6
            }

要獲得所有IP4和IP6地址,這是我的首選解決方案。 請注意,它還會過濾環回IP地址,如127.0.0.1或:: 1

public static IEnumerable<IPAddress> GetIpAddress()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            return (from ip in host.AddressList where !IPAddress.IsLoopback(ip) select ip).ToList();
        }

這是我獲取所有IPv4地址的方法。

    /// <summary>
    /// Gets/Sets the IPAddress(s) of the computer which the client is running on.
    /// If this isn't set then all IPAddresses which could be enumerated will be sent as
    /// a comma separated list.  
    /// </summary>
    public string IPAddress
    {
        set
        {
            _IPAddress = value;
        }
        get
        {
            string retVal = _IPAddress;

            // If IPAddress isn't explicitly set then we enumerate all IP's on this machine.
            if (_IPAddress == null)
            {
                // TODO: Only return ipaddresses that are for Ethernet Adapters

                String strHostName = Dns.GetHostName();
                IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
                IPAddress[] addr = ipEntry.AddressList;

                List<string> validAddresses = new List<string>();

                // Loops through the addresses and creates a list of valid ones.
                for (int i = 0; i < addr.Length; i++)
                {
                    string currAddr = addr[i].ToString();
                    if( IsValidIP( currAddr ) ) {
                        validAddresses.Add( currAddr );
                    }
                }

                for(int i=0; i<validAddresses.Count; i++)
                {
                    retVal += validAddresses[i];
                    if (i < validAddresses.Count - 1)
                    {
                        retVal += ",";
                    }
                }

                if (String.IsNullOrEmpty(retVal))
                {
                    retVal = String.Empty;
                }

            }

            return retVal;
        }
    }

暫無
暫無

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

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