【问题标题】:How do I get my current DNS Server in C#?如何在 C# 中获取当前的 DNS 服务器?
【发布时间】:2010-05-25 16:55:35
【问题描述】:

如何在 C# 中获取我当前的 DNS 服务器?

【问题讨论】:

  • 您的意思是本地机器的 DNS 服务器、您的域的 DNS 服务器还是其他?对于什么是“您的 DNS 服务器”,IMO 有很多不同的答案。
  • 我正在考虑将执行 Mx Record 查找的不安全代码转换为安全版本。见:stackoverflow.com/questions/2906615/…
  • 我想知道的是如何在不指定dns服务器名称的情况下进行DNS查找,即让系统使用默认值。每当您在某处输入 URL 时都会发生这种情况,因此不确定为什么 DNS 类不允许使用默认值。

标签: c#


【解决方案1】:

有关示例代码,请参阅 IPInterfaceProperties.DnsAddresses 上的 MSDN。

【讨论】:

    【解决方案2】:

    我最近也在尝试做同样的事情,发现了 Robert Sindal 的 excellent example

    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");
            }
        }
    }
    

    【讨论】:

    • 对于我自己,因为我实际上是通过 powershell 执行此操作的(在没有 ipconfig 等的地方)[System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() | ? { $_.OperationalStatus -eq [System.Net.NetworkInformation.OperationalStatus]::Up } | % { $_.GetIPProperties().DnsAddresses.IPAddress
    【解决方案3】:

    此方法确定所有“up”接口上配置的 dns 服务器地址。它允许选择是否需要 IPv4 和/或 IPv6 地址。

    using System.Net.NetworkInformation;
    using System.Net.Sockets;
    
    public static IPAddress[] GetDnsAdresses(bool ip4Wanted, bool ip6Wanted)
    {
        NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
        HashSet<IPAddress> dnsAddresses = new HashSet<IPAddress>();
    
        foreach (NetworkInterface networkInterface in interfaces)
        {
            if (networkInterface.OperationalStatus == OperationalStatus.Up)
            {
                IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
    
                foreach (IPAddress forAddress in ipProperties.DnsAddresses)
                {
                    if ((ip4Wanted && forAddress.AddressFamily == AddressFamily.InterNetwork) || (ip6Wanted && forAddress.AddressFamily == AddressFamily.InterNetworkV6))
                    {
                        dnsAddresses.Add(forAddress);
                    }
                }
            }
        }
    
        return dnsAddresses.ToArray();
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-22
      • 2011-03-05
      • 2010-09-08
      • 2012-05-18
      • 2012-09-17
      • 2016-12-26
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      相关资源
      最近更新 更多