Get IP from IPInterfaceProperties
-
Hi, I'm trying to get the IP, subnet and gateway for my interfaces using IPInterfaceProperties but both UnicastAddresses and GatewayAddresses returns a collection... How do I know which one to use? Here is my loop and for now I'm just using UnicastAddresses[0] and GatewayAddresses[0] but that isn't always right
internal static NetworkAdapter[] AddExtraInformation(ArrayList adapters)
{
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkAdapter adapter in adapters)
{
foreach (NetworkInterface networkInterface in networkInterfaces)
{
if (!DescriptionToName(networkInterface.Description).Equals(adapter.DeviceName))
continue;adapter.Name = networkInterface.Name; adapter.OperationalStatus = networkInterface.OperationalStatus.ToString(); IPInterfaceProperties ipProperties = networkInterface.GetIPProperties(); if (ipProperties.UnicastAddresses != null) if (ipProperties.UnicastAddresses.Count > 0) { adapter.Address = ipProperties.UnicastAddresses\[0\].Address.ToString(); adapter.SubnetMask = ipProperties.UnicastAddresses\[0\].IPv4Mask.ToString(); } if (ipProperties.GatewayAddresses != null) if (ipProperties.GatewayAddresses.Count > 0) adapter.Gateway = ipProperties.GatewayAddresses\[0\].Address.ToString(); break; } } return (NetworkAdapter\[\])adapters.ToArray(typeof(NetworkAdapter));
}
http://johanmartensson.se - Home of MPEG4Watcher http://www.tinywebradio.com - Home of TinyWebRadio
-
Hi, I'm trying to get the IP, subnet and gateway for my interfaces using IPInterfaceProperties but both UnicastAddresses and GatewayAddresses returns a collection... How do I know which one to use? Here is my loop and for now I'm just using UnicastAddresses[0] and GatewayAddresses[0] but that isn't always right
internal static NetworkAdapter[] AddExtraInformation(ArrayList adapters)
{
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkAdapter adapter in adapters)
{
foreach (NetworkInterface networkInterface in networkInterfaces)
{
if (!DescriptionToName(networkInterface.Description).Equals(adapter.DeviceName))
continue;adapter.Name = networkInterface.Name; adapter.OperationalStatus = networkInterface.OperationalStatus.ToString(); IPInterfaceProperties ipProperties = networkInterface.GetIPProperties(); if (ipProperties.UnicastAddresses != null) if (ipProperties.UnicastAddresses.Count > 0) { adapter.Address = ipProperties.UnicastAddresses\[0\].Address.ToString(); adapter.SubnetMask = ipProperties.UnicastAddresses\[0\].IPv4Mask.ToString(); } if (ipProperties.GatewayAddresses != null) if (ipProperties.GatewayAddresses.Count > 0) adapter.Gateway = ipProperties.GatewayAddresses\[0\].Address.ToString(); break; } } return (NetworkAdapter\[\])adapters.ToArray(typeof(NetworkAdapter));
}
http://johanmartensson.se - Home of MPEG4Watcher http://www.tinywebradio.com - Home of TinyWebRadio
string hostName = Dns.GetHostName(); IPHostEntry ipEntry = Dns.GetHostEntry(hostName); foreach (IPAddress ip in ipEntry.AddressList) { //Please check ip values } For example, if you want to get IPv4 value, use "ip" variable which is using foreach statement; if (ip.AddressFamily.Equals(System.Net.Sockets.AddressFamily.InterNetwork)) string result = ip.ToString(); Best Regard...
-
string hostName = Dns.GetHostName(); IPHostEntry ipEntry = Dns.GetHostEntry(hostName); foreach (IPAddress ip in ipEntry.AddressList) { //Please check ip values } For example, if you want to get IPv4 value, use "ip" variable which is using foreach statement; if (ip.AddressFamily.Equals(System.Net.Sockets.AddressFamily.InterNetwork)) string result = ip.ToString(); Best Regard...
Thanks for your reply I'm not sure how to tell which nic has the ip-number that I get from using your code. Anyway, I changed my code to this, looping the addresses and checking for IsDnsEligible, that seems to do the trick or am I missing something?
foreach (UnicastIPAddressInformation address in ipProperties.UnicastAddresses)
{
if(address.IsDnsEligible)
{
adapter.Address = address.Address.ToString();
adapter.SubnetMask = address.IPv4Mask.ToString();
break;
}
}http://johanmartensson.se - Home of MPEG4Watcher http://www.tinywebradio.com - Home of TinyWebRadio