所以我们都知道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/