The question "How to get the IP address?" maybe seems topical and already answered in S.O., but I'm having troubles in the circunstances that I explain below.
I'm writting a simple helper class to provide miscellaneous networking utilities that will help me in my development. I would like to write a function that will return the Ipv4 or Ipv6 of the current machine, the same Ip that is used when I navigate through a web browser, you know... just the relevant Ip of the user, and I would like to do it without queryng to a random site to discover the Ip address that is used.
For most people the task would be simple as this:
(From ip As IPAddress In Dns.GetHostEntry(Dns.GetHostName).AddressList()
Where ip.AddressFamily = AddressFamily.InterNetwork).FirstOrDefault
Or by iterating the System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
interfaces to do the same, however, I found that methodology is not efficient in most scenarios...
In my system I have 4 adapters, from those 4 adapters 2 are virtual adapters created by VMWare WorkStation (which are threated as Ethernet adapters), one virtual adapter from TAP-Win32 driver, and the "real" or physical Ethernet adapter that I use to navigate through Internet.
So, in C# or VB.Net, how can I Identify the proper adapter to retrieve the real Ip that is used to connect to Internet? the same Ip that returns websites like this?.
Note that when I ask "how to identify the proper adapter" what I'm asking is "how to identify the preferred adapter that the system will use to connect to Internet?", because an user could have many physical Ethernet adapters but only one will be the "preferred" to connect to Internet, then... how to do this?.