c# - How to get IPv4 and IPv6 address of local machine? -


I am developing a windows application and I need to find local machine's IPv4 and IPv6 addresses. Can be OS X or Windows 7.

I got a solution to get the MAC address,

  string gatemacadder () {var macAddr = (in the network interface) .NETNetwork Interface () where nic .operationalStatus == OperationalStatus.Up Select nic.GetPhysicalAddress (). ToString ()). FirstOrDefault (); Return macAddr.ToString (); }   

This is working in all OS

What is the correct way to work on IPv4 and IPv6 addresses and Windows 7?

  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 (edit [0] .adform == System.Net.Sockets.AddressFamily.InterNetworkV6) {Console.WriteLine (addr [0] .ToString ()); // ipv6}    

Comments