I was working on some code recently where I needed to get the IP Address of the current machine that the code was executing on. I settled on this snippet below. I thought I would share it.
using System; using System.Collections.Generic; using System.Linq; using System.Net; namespace Utility { public sealed class MachineIPAddress { private MachineIPAddress() { } public static string RetrieveIPAddress { get { var hostEntry = Dns.GetHostEntry(Dns.GetHostName()); var ip = (from addr in hostEntry.AddressList where String.Compare(addr.AddressFamily.ToString(), "InterNetwork", StringComparison.OrdinalIgnoreCase) == 0 select addr.ToString() ).FirstOrDefault(); return ip; } } } }
