Подтвердить что ты не робот

Как получить текущий DNS-сервер в С#?

Как получить мой текущий DNS-сервер в С#?

4b9b3361

Ответ 2

Недавно я пытался сделать то же самое и нашел это отличный пример Роберта Синдала.

using System;
using System.Net;
using System.Net.NetworkInformation;

namespace HowToGetLocalDnsServerAddressConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(GetDnsAdress());
            Console.ReadKey();
        }

        private static IPAddress GetDnsAdress()
        {
            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface networkInterface in networkInterfaces)
            {
                if (networkInterface.OperationalStatus == OperationalStatus.Up)
                {
                    IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
                    IPAddressCollection dnsAddresses = ipProperties.DnsAddresses;

                    foreach (IPAddress dnsAdress in dnsAddresses)
                    {
                        return dnsAdress;
                    }
                }
            }

            throw new InvalidOperationException("Unable to find DNS Address");
        }
    }
}