检索连接的路由器IP地址:NetworkInterface.GetAllNetworkInterfaces() [英] Retrieve Connected Router IP Address: NetworkInterface.GetAllNetworkInterfaces()
问题描述
我正在尝试获取Retrieve Connected Router IP地址。我可以看到地址并ping地址,但是我无法获取地址或查看地址。有人可以给出他们的想法或给我一些帮助。这也是我的逻辑。
在下面的逻辑中,连接的路由器不会显示在列表中。
I am trying to get the Retrieve Connected Router IP Address. I can see the address and ping the address, however I am not able to get the address or see the address. Can someone give their thoughts or give me some assistance. Here is my logic below also. In the logic below the connected router do not show up in the list.
var nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (var nic in nics)
{
var ippropoerties = nic.GetIPProperties();
//we are only interested in IPv4 address
var ipv4add = ippropoerties.UnicastAddresses.Where(addr => addr.Address.AddressFamily == AddressFamily.InterNetwork);
foreach (var addr in ipv4add)
{
var network = CalculateNetwork(addr);
Console.WriteLine("Addr: {0} Mask: {1} Network: {2}", addr.Address, addr.IPv4Mask, network);
string retrivedip = addr.Address.ToString();
string local_host = Dns.GetHostName(); // returns local hostname
IPHostEntry host_entry = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host_entry.AddressList)
{
}
}
}
pianoboyCoder
pianoboyCoder
推荐答案
您的代码显示了与机器关联的每个网络地址的一些属性,然后获取它的DNS名称(机器)。这些都与网络上的其他机器无关,因为它与当前的机器有关。
Your code is displaying some properties about each network address associated with the machine and then getting the DNS name for it (the machine). None of that has anything to do with other machines on the network as it is all about the current machine.
无法保证有路由器,但对于许多网络而言,"路由器"都是如此。是默认网关,因此如果您做出该假设,则可以使用IPInterfaceProperties类来获取GatewayAddress。在上面的代码中,我相信是ipproperties
变量。
There are no guarantees there is a router but for many networks the "router" is the default gateway so if you make that assumption then you can use the IPInterfaceProperties class to get the GatewayAddress. In your code above that is the ipproperties variable I believe.
Michael Taylor
http://www.michaeltaylorp3.net
Michael Taylor
http://www.michaeltaylorp3.net
这篇关于检索连接的路由器IP地址:NetworkInterface.GetAllNetworkInterfaces()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!