Filtering list returned by NetworkInterface.GetAllNetworkInterfaces
The NetworkInterface.GetAllNetworkInterfaces method returns a list of all interfaces on the system, but it returns a lot of seemingly garbage interfaces too like
xxxx::xxxx:xxxx:xxxx:xxxx%12
::1
in add开发者_如何学Cition to "normal" ones like
127.0.0.1
192.168.0.3
etc
I only want keep these "normal" ones. What criteria (properties, methods) should I use for this?
The 'garbage' ones are IPv6 addresses. They're goodness. But if you only need the IPv4 ones, do:
var list = NetworkInterface
.GetAllNetworkInterfaces()
.Where(n => n.GetIPProperties().UnicastAddresses.First().Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
Have a look at using the LINW Where method.
Something like
var yourList = NetworkInterface.GetAllNetworkInterfaces().Where(x => /*your boolean expression here>*/)
精彩评论