IP address reading

A simple code that reads the IP addresses from the computer that runs our application and returns the one that’s not the loopback address. If a computer has several network cards, return from the method with the one you want by changing the if statement to result = ipv4Address[{requestedID}]

private IPAddress GetLocalIPAddress()
        {
            IPAddress result = null;
            IPHostEntry iphostentry = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress[] ipv4Address = Array.FindAll(iphostentry.AddressList, add => add.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !IPAddress.IsLoopback(add));
            if (ipv4Address.Length > 0) result = ipv4Address[0];
 
            return result;
        }

Do not forget to add the System.Net namespace.