![]() ![]() The above solution will give you a valid/connected interface, and is good enough for 99% of situations. ![]() ![]() The latter one can be used to filter out 0.0.0.0 addresses. You can check the AddressFamily one to distinguish between IPv4 and IPv6. I've also added some further commented checks which have been pointed out as useful by other people here. Where(a => Array.FindIndex(a.GetAddressBytes(), b => b != 0) >= 0) Where(a => a.AddressFamily = AddressFamily.InterNetwork) SelectMany(n => n.GetIPProperties()?.GatewayAddresses) Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback) Where(n => n.OperationalStatus = OperationalStatus.Up) It will probably be the first valid and enabled gateway address of the first enabled network interface: public static IPAddress GetDefaultGateway() ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |