using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; namespace DnsClient { public class NameServer { /// /// The default DNS server port. /// public const int DefaultPort = 53; /// /// Gets a list of name servers by iterating over the available network interfaces. /// /// The list of name servers. public static ICollection ResolveNameServers() { var result = new HashSet(); var adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface networkInterface in adapters .Where(p => p.OperationalStatus == OperationalStatus.Up && p.NetworkInterfaceType != NetworkInterfaceType.Loopback)) { foreach (IPAddress dnsAddress in networkInterface .GetIPProperties() .DnsAddresses .Where(i => i.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork || i.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)) { result.Add(new IPEndPoint(dnsAddress, DefaultPort)); } } return result.ToArray(); } } }