I’m writing a program that shows the user their IP address, Subnet mask and Default gateway. I can get the first two, but for the last one, this is what I turned up:
GatewayIPAddressInformationCollection gwc =
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].GetIPProperties().GatewayAddresses;
That, of course, returns a collection of GatewayIPAddressInformation. So, if a computer has multiple gateways, how can I determine which is the default gateway?
In practice, I’ve only ever seen this collection contain a single entry, but since it’s implemented as a collection, it stands to reason that some computers contain multiple gateways, none of which are marked as “Default”. So is there a way to determine the default or is it all just guesswork?
It will probably be the first valid and enabled gateway address of the first enabled network interface:
I’ve also added some further commented checks which have been pointed out as useful by other people here. You can check the
AddressFamilyone to distinguish between IPv4 and IPv6. The latter one can be used to filter out 0.0.0.0 addresses.The above solution will give you a valid/connected interface, and is good enough for 99% of situations. That said, if you have multiple valid interfaces that traffic can be routed through, and you need to be 100% accurate, the way to do this uses
GetBestInterfaceto find an interface for routing to a specific IP address. This additionally handles the case where you might have a specific address range routed through a different adapter (e.g.10.*.*.*going through a VPN, everything else going to your router)These two examples could be wrapped up into a helper class and used in the appropriate cases: that you do, or do not have a destination address in mind already.