1

I am trying to get IP address on local machine:

    private string GetIP()
    {

        string strHostName = "";
        strHostName = System.Net.Dns.GetHostName();

        IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);


        IPAddress[] addr = ipEntry.AddressList;

        foreach (IPAddress ipaddr in addr)
        {
            if (ipaddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                return ipaddr.ToString();
        }

        return "IP error";                                   
    }

However I can't find a way to identify which interface is the one i need. For example:

enter image description here

I am lucky that the one i need is second in the list. But if it were in the back i would get IP of a wrong interface. How to check if I am getting IP for local area connection (or in general, the interface responsible for the connection).

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
and
  • 87
  • 1
  • 9
  • How do you expect your program to know which NIC it should be looking at? – M.Babcock Jan 31 '12 at 13:42
  • That's what i don't know how to implement. Now it just takes the first InterNetwork in the list. I want to somehow verify that it is local area connection. – and Jan 31 '12 at 13:51
  • What are all of the other IPs in your list if they don't belong to local area connections? – M.Babcock Jan 31 '12 at 13:52
  • I cannot see the image (stupid web filter), but I am assuming the two are on different subnets. Couldn't you filter the list by either ip addresses that are in an acceptable subnet or ones that are not in an un-acceptable subnet? – Zach Green Jan 31 '12 at 13:52
  • It's virtual interfaces. I don't really know how my network is configured to be honest. – and Jan 31 '12 at 13:55

2 Answers2

2

You may be able to enumerate the network interfaces directly (rather than just their IPs) and filter then based on their interface type:

var interfaces = NetworkInterface.GetAllNetworkInterfaces()

And then filter it with something like:

interfaces.Where(ni => ni.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
                       ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel)

It may still return multiple network interfaces but it'll filter out at least some of them that you don't want. I use the above filter to get rid of loopback and virtual machine interfaces.

Then from there you can get the network interface's IP address using the IP properties.

In the spirit of brevity, once you determine which interface is the right one, you can get the IPv4 address (or at least one of them) of the interface using:

iface.GetIPProperties().UnicastAddresses.SingleOrDefault(ua => ua.Address.AddressFamily == AddressFamily.InterNetwork);
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
  • I tried this before, however (manually choosing the right interface) IP properties gets me the wrong IP. It gets IP of a gateway. – and Jan 31 '12 at 14:15
  • Then you're not using the right property from IP properties. Post the code you used and we can help you with it. Here's a hint, the addresses you care about here are typically called *unicast* addresses. – M.Babcock Jan 31 '12 at 14:16
  • IPAddressCollection ips = currentNic.GetIPProperties().DnsAddresses; wrong one? – and Jan 31 '12 at 14:25
  • This is still filtering ua => ua.Address.AddressFamily == AddressFamily.InterNetwork – Massimiliano Peluso Feb 01 '12 at 11:06
  • @MassimilianoPeluso - It filters the address family after previously filtering out loopback and tunnel interfaces so the address list returned should be limited a great deal as compared to getting *all* of the addresses and then only checking for IPv4. – M.Babcock Feb 01 '12 at 12:47
  • I know why you are filtering for my comment was for @user1170136 which was looking for native method that returns on single address – Massimiliano Peluso Feb 01 '12 at 13:20
0

There is not method that return one address against the host name

IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());

it returns the local machine address for all the registered address in your DNS

So if in your DNS your machine has one name associated to one IP address it will return only that address otherwise it will return the list of addresses associated to that Host Name

you have to "filter" the list to understand what is your local address

Have a look at the below:

How to get the IP address of the server on which my C# application is running on?

Community
  • 1
  • 1
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
  • 1
    For me that returns the same set of addresses as with `GetHostEntry()`. – svick Jan 31 '12 at 13:45
  • they return the same information but they behave differently http://msdn.microsoft.com/en-us/library/ms143998(v=vs.100).aspx The GetHostAddresses method has different behavior with respect to IP literals. If step #1 above succeeds (it successfully parses as an IP address), that address is immediately returned as the result. There is no attempt at a reverse lookup. – Massimiliano Peluso Jan 31 '12 at 13:51
  • What sivick said. Either GetHostAddresses() or GetHostEntry() i get a list. – and Jan 31 '12 at 13:52
  • yes they do as they return all the addresses associated to the Host Name.I have edited my answer to make it more clear – Massimiliano Peluso Jan 31 '12 at 13:54
  • This still doesnt help me to filter that list :/ – and Jan 31 '12 at 14:16
  • There is not method that returns 1 Address What's the problem of filtering? – Massimiliano Peluso Jan 31 '12 at 14:16