2012-07-10 108 views
4

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

我得到了一個解決方案,讓MAC地址一樣,

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

    return macAddr.ToString(); 
} 

這在所有OS工作。

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

回答

5
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 
      } 
+0

我試過了。但是它只返回IPv4 – Matt 2012-07-10 10:40:36

+0

addr [0]有ipv6,查看編輯答案 – Habib 2012-07-10 10:43:14

+0

感謝您的寶貴信息。但IPv6返回:: 1。有什麼方法可以獲得實際價值嗎? – Matt 2012-07-10 12:07:59

0

這裏是我的方法只獲取所有的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; 
     } 
    } 
+0

他想要IPv4和IPv6地址。 – 2012-07-10 10:52:57

+0

@Ramhound ......是的,這很清楚。可以很容易地修改爲執行IPv6。 – blak3r 2012-07-12 00:40:56

+2

這就是我低估了你的答案的原因:**「這是我的方法,只能獲取所有的IPv4地址。」**你沒有指出如何修改代碼來獲得IPv6地址。 – 2012-07-12 11:30:34

1

要得到所有的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(); 
     } 
+0

注意:這需要使用'System.Linq'和'System.Collections.Generic'命名空間。 – starbeamrainbowlabs 2016-10-15 09:38:54

相關問題