Skip to content

Instantly share code, notes, and snippets.

@pelengami
Created February 19, 2017 18:01
Show Gist options
  • Save pelengami/a735bd492255fed7c79146b3adba6848 to your computer and use it in GitHub Desktop.
Save pelengami/a735bd492255fed7c79146b3adba6848 to your computer and use it in GitHub Desktop.
Get Available Ip Addresses
public static IEnumerable<IPAddress> GetIpAddressesFromNetworkAdapters(AddressFamily addressFamily)
{
var networkInterfaces = from networkInterface in NetworkInterface.GetAllNetworkInterfaces()
where networkInterface.OperationalStatus == OperationalStatus.Up &&
networkInterface.SupportsMulticast &&
(networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
select new
{
name = networkInterface.Name,
ip = GetIpFromUnicastAddresses(networkInterface, addressFamily)
};
return networkInterfaces.Select(x => x.ip);
}
private static IPAddress GetIpFromUnicastAddresses(NetworkInterface networkInterface, AddressFamily addressFamily)
{
return (from unicastIpAddressInfo in networkInterface.GetIPProperties().UnicastAddresses
where unicastIpAddressInfo.Address.AddressFamily == addressFamily
select unicastIpAddressInfo.Address).SingleOrDefault();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment