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:

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).
You may be able to enumerate the network interfaces directly (rather than just their IPs) and filter then based on their interface type:
And then filter it with something like:
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: