c# - 如何知道网络接口(interface)何时在 C# 中使用路由器作为 DNS 服务器?

标签 c# .net windows dns router

所以我们都知道cmd命令'ipconfig/all'。例如,我们有 Wi-Fi 网络接口(interface)。启用 DHCP,DNS 服务器是我们自己的路由器。我怎么知道网络接口(interface)是否使用路由器作为 C# 中的 DNS 服务器?在代码中,我可以获得 DNS 服务器 IP 和所有其他信息,但是是否可以知道路由器是否作为 DNS 服务器?

默认类 NetworkInterface 可能不会告诉我...但也许有某种算法可以确定这一点?

最佳答案

NetworkInterface 类公开了大量数据,如接口(interface)网关和 dns 服务器 ip。只需要检查gateway ip == dns ip。每个接口(interface)可以有多个网关和 dns,所以你需要自己找到一些规则。在那里,我天真的实现基于这个 SO :

NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

foreach (NetworkInterface networkInterface in networkInterfaces)
{
    if (networkInterface.OperationalStatus == OperationalStatus.Up && networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback)
    {
        IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();

        var gateways = ipProperties.GatewayAddresses.Select(ga => ga.Address);
        var dns = ipProperties.DnsAddresses;

        if (dns.Any(d => gateways.Contains(d))){
            Console.Write("dns router found on interface " + networkInterface.Name);
        }
    }
}

关于c# - 如何知道网络接口(interface)何时在 C# 中使用路由器作为 DNS 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54062459/

相关文章:

c# - 为测试生成实体对象的常用方法

c# - 使用 C# 和 .NET 进行审计

windows - 如何在 Windows 开发环境中使用 OpenSSL for Rust

c# - 捕获 SQL 连接的信息消息有什么好处?

c# - 带有重复项的日历

c# - 代码审查 : CLR RegexSubstring

.net - WPF 应用程序 : get 3rd party binaries from install on target machine

c# - .NET 在运行时再次加载程序集

windows - Windows Azure 中的 URL 重写

windows - 环境的 Lua 注册表?