In the program you need to get your ip address. I do this with this function:

public static string GetLocalIPAddress() { var host = Dns.GetHostEntry(Dns.GetHostName()); foreach (var ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { return ip.ToString(); } } return null; } 

But there is the following problem: If a virtual network adapter is installed on the machine (for example, from VirtualBox), then the function displays its ip-address.

Is it possible to somehow filter the virtual adapters?

    1 answer 1

    Add a link to System.Management and use the WMI query for the Win32_NetworkAdapter class, real interfaces should have PhysicalAdapter = true . To obtain the Guid interface, you can use NetworkInterface.GetAllNetworkInterfaces() . Something like this:

     using System.Net; using System.Net.Sockets; using System.Net.NetworkInformation; using System.Management; ... //Определяет, является ли адаптер физическим public static bool IsAdapterPhysical(string guid) { ManagementObjectCollection mbsList = null; ManagementObjectSearcher mbs = new ManagementObjectSearcher( "SELECT PhysicalAdapter FROM Win32_NetworkAdapter WHERE GUID = '" + guid + "'" ); bool res = false; using (mbs) { mbsList = mbs.Get(); foreach (ManagementObject mo in mbsList) { foreach (var p in mo.Properties) { if (p.Value != null) { res = (bool)p.Value; break; } else res = false; } } return res; } } //Получает все локальные IP-адреса public static List<IPAddress> GetIpAddresses() { List<IPAddress> res = new List<IPAddress>(10); var ifs = NetworkInterface.GetAllNetworkInterfaces(); foreach (var interf in ifs) { var ipprop=interf.GetIPProperties(); if (ipprop == null) continue; var unicast = ipprop.UnicastAddresses; if (unicast == null) continue; if (IsAdapterPhysical(interf.Id.ToString())) { //находим первый Unicast-адрес foreach (var addr in unicast) { if (addr.Address.AddressFamily != AddressFamily.InterNetwork) continue; res.Add(addr.Address); break; } } } return res; }