//Returns an IP Address based on version requested
private string GetIPAddress(int version)
{
string address = null;
var IPAddressHashSet = new HashSet<IPAddress>(Dns.GetHostAddresses(GetHostName()));
switch (version)
{
case 4:
//Assign ipv4 address as string to "address"
break;
case 6:
//Assign ipv4 address as string to "address"
break;
default:
address = null;
break;
}
return address;
}
As you can see I have created a HashSet called "IPAddressHashSet"
Depending on the version of IP requested I would like to return either the local ipv4 or ipv6 address.
I think that I could do this by getting each IP Address in the HashSet and comparing it to see if it fits a certain frame (i.e. "{0:###.###.###.###}") but even then there would be no way to determine if that address was the local address, external address, network device etc.
In this case I would just be looking for a basic "192.168.0.###", but even that wouldn't work because not all default gateways would start with 192.168...
Are there any .NET classes useful for this? Essentially I would like to determine the version of the IP Address and return the local address of the current machine on its default gateway.