1
    //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.

Jojo
  • 11
  • 4
  • 1
    `IPAddress.Parse` might help you out – JohanP Nov 20 '17 at 04:34
  • 1
    If you're concerned about not knowing the network topology ("not all default gateways would start with 192.168.") then you may have to consider that more exotic situations can also exist where a machine is connected to multiple networks. Without knowing how you intend to *use* this address, the answer may be any of there being *no* such "local" address, exactly one or *more than one*. So what's the actual use for the returned value? – Damien_The_Unbeliever Nov 20 '17 at 07:45
  • I resolved this issue by using NetworkInterface.GetAllInterfaces().ToList(); I could then use that list to specify an adapter. So the machine can then get the IPv4 and IPv6 addresses tied to each interface. – Jojo Nov 20 '17 at 15:18

1 Answers1

1

You can use IPAddress.Family for that. It's InterNetwork for IPv4 and InterNetworkV6 for IPv6. If you want to filter out external ips - check this answer with sample code illustrating how to detect if ip belongs to private range. Sample code:

private IPAddress GetFirstPrivateIp(bool ipV4) {
    var interfaces = NetworkInterface.GetAllNetworkInterfaces();
    return interfaces
        .SelectMany(c => c.GetIPProperties().UnicastAddresses)
        .Where(c => 
            !IPAddress.IsLoopback(c.Address)
            && c.Address.AddressFamily == (ipV4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6)
            && (!ipV4 || IpIsPrivate(c.Address))) // IpIsPrivate - refer to question linked above
        .Select(c => c.Address)
        .FirstOrDefault();
}
Evk
  • 98,527
  • 8
  • 141
  • 191