-
-
Save pelengami/a735bd492255fed7c79146b3adba6848 to your computer and use it in GitHub Desktop.
Get Available Ip Addresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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