6

I'm wondering if there's a reliable way of finding the IPv4 address of the network adapter in my machine which is used to access the internet (since this is the one I'd like to bind my server to). I used to get a list of local ip addresses like this:

IPAddress ip = System.Net.Dns.GetHostByName(Environment.MachineName).AddressList[0];

And it worked fine but today it failed because the IP address I was looking for was not the first one in this address list but the 3rd one (since I had 2 virtual machines running and both of these created a virtual adapter).

Any advice would be much appreciated.

beta
  • 2,583
  • 15
  • 34
  • 46
  • 1
    Perhaps the answers on this question might help: http://stackoverflow.com/q/2172962/1220971 – Bridge Dec 05 '12 at 14:55
  • Thanks +1 for the link, I'm going to check out the GetBestInterface API function. – beta Dec 05 '12 at 15:20
  • possible duplicate of [Identifying active network interface in .NET](http://stackoverflow.com/questions/359596/identifying-active-network-interface-in-net) – Jon B Dec 05 '12 at 16:02
  • Not a duplicate (one has a known IPv4 address; The other does not.) – finnw Dec 06 '12 at 18:06

1 Answers1

9
IPAddress ip = System.Net.Dns.GetHostEntry(Environment.MachineName).AddressList.Where(i => i.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).FirstOrDefault();

As an alternative way, you could use:

using System.Net.NetworkInformation;

//...

var local = NetworkInterface.GetAllNetworkInterfaces().Where(i => i.Name == "Local Area Connection").FirstOrDefault();
var stringAddress = local.GetIPProperties().UnicastAddresses[0].Address.ToString();
var ipAddress = IPAddress.Parse(stringAddress);

where you just have to replace the "Local Area Connection" with the name of your adapter in the Control Panel\Network and Internet\Network Connections

sabvente
  • 144
  • 8
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
  • 1
    Unfortunately this approach returns one of the virtual adapters' addresses as well. – beta Dec 05 '12 at 15:19
  • Well, what would you choose if you would have multiple network interfaces and each one of them would be connected to the internet? – Alex Filipovici Dec 05 '12 at 15:27
  • If I mouse over the network icon in the tray bar in Windows it says something like "Ethernet-Adapter, internet access" which is the only physical adapter in my machine. I'd like my application to find exactly this adapter and use its address. Unfortunately all approaches I tried so far return the address of one of the virtual adapters, though. – beta Dec 05 '12 at 15:33
  • 1
    how about the last approach? – Alex Filipovici Dec 05 '12 at 15:55
  • Thanks for this one, however I already managed to make it work using this solution: http://stackoverflow.com/questions/359596/identifying-active-network-interface-in-net/621702#621702 Marked as answer for your active support and effort, though! – beta Dec 05 '12 at 15:57
  • 1
    In your LINQ Query: you can replace the "Where(condition)" with "FirstOrDefault(condition)". There is no difference in function but the calling is a little bit shorter. Thanks for the answer btw ;) – Ulfhetnar Mar 29 '22 at 12:47