GetHostEntry?

A

ad

Hi,
I use the code below to get the IP of computer:
My computer has only one ip: 192.168.0.100,
But there are tree IPs get by GetHostEntry:
192.168.0.100
192.168.0.1
192.168.122.1

Why?




//--------------------------------------------------------------------------------------
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
 
N

Nicholas Paldino [.NET/C# MVP]

When you call GetHostEntry, it returns the IP addresses that are mapped
to that DNS name, not necessarily the IP addresses a local machine has.

A better way to get this information (which doesn't rely on entries in
the DNS table) would be to use the classes in the System.Management
namespace to query all the network adapters on your machine for the
instances of the Win32_NetworkAdapterConfiguration WMI class. Then, you can
access the IPAddresses property on the instances to get the ip addresses
that are allocated to each network adapter.

Hope this helps.
 
A

ad

Thanks,
Could you give me an example?

Nicholas Paldino said:
When you call GetHostEntry, it returns the IP addresses that are mapped
to that DNS name, not necessarily the IP addresses a local machine has.

A better way to get this information (which doesn't rely on entries in
the DNS table) would be to use the classes in the System.Management
namespace to query all the network adapters on your machine for the
instances of the Win32_NetworkAdapterConfiguration WMI class. Then, you
can access the IPAddresses property on the instances to get the ip
addresses that are allocated to each network adapter.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ad said:
Hi,
I use the code below to get the IP of computer:
My computer has only one ip: 192.168.0.100,
But there are tree IPs get by GetHostEntry:
192.168.0.100
192.168.0.1
192.168.122.1

Why?




//--------------------------------------------------------------------------------------
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
 
W

Willy Denoyette [MVP]

using System;
using System.Management;
using System.Runtime.InteropServices;
using System.Threading;
public class Wmis {
public static void Main()
{
// query only IP enabled Adapters
using(ManagementObjectSearcher query = new
ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration
"))
{
ManagementObjectCollection queryCollection = query.Get();
foreach( ManagementObject mo in queryCollection )
{
string[] addresses = (string[])mo["IPAddress"];
if((bool)mo["IpEnabled"]) {
foreach(string s in addresses)
Console.WriteLine( "IP Address '{0}'", s);
}
}
}
}
}

Willy.

ad said:
Thanks,
Could you give me an example?

Nicholas Paldino said:
When you call GetHostEntry, it returns the IP addresses that are
mapped to that DNS name, not necessarily the IP addresses a local machine
has.

A better way to get this information (which doesn't rely on entries in
the DNS table) would be to use the classes in the System.Management
namespace to query all the network adapters on your machine for the
instances of the Win32_NetworkAdapterConfiguration WMI class. Then, you
can access the IPAddresses property on the instances to get the ip
addresses that are allocated to each network adapter.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ad said:
Hi,
I use the code below to get the IP of computer:
My computer has only one ip: 192.168.0.100,
But there are tree IPs get by GetHostEntry:
192.168.0.100
192.168.0.1
192.168.122.1

Why?




//--------------------------------------------------------------------------------------
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top