2012-07-10 5 views
4

Windows 응용 프로그램을 개발 중이며 로컬 컴퓨터의 IPv4 및 IPv6 주소를 찾아야합니다. 이 모든 OS에서 작동하고로컬 컴퓨터의 IPv4 및 IPv6 주소를 얻는 방법은 무엇입니까?

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

    return macAddr.ToString(); 
} 

OS는, 내가 좋아하는 MAC 주소를 가져 오기위한 솔루션을 가지고 XP 또는 Windows 7

수 있습니다.

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

+0

addr [0]은 ipv6을 가지고 있습니다. 편집 된 대답을 확인하십시오. – Habib

+0

감사합니다. 그러나 IPv6 반환 :: 1. 실제 가치를 얻을 수있는 방법이 있습니까? – Matt

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 주소를 모두 원합니다. –

+0

@Ramhound ... 네, 그건 분명히 진술되었습니다. IPv6을 쉽게 수정할 수 있습니다. – blak3r

+2

이것이 제가 대답을 떨어 뜨린 이유입니다. ** "모든 IPv4 주소를 얻는 방법은 여기 있습니다."** IPv6 주소를 얻기 위해 코드를 수정하는 방법을 지정하지 않았습니다. –

1

모든 IP4 및 IP6 주소를 얻으려면 여기를 클릭하십시오. 127.0.0.1 또는 :: 1과 같은 루프백 IP 주소도 필터링한다는 점에 유의하십시오.

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

관련 문제