简体   繁体   English

NetworkInterface.GetAllNetworkInterfaces返回的筛选列表

[英]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 NetworkInterface.GetAllNetworkInterfaces方法返回系统上所有接口的列表,但它也返回许多看似垃圾的接口,例如

xxxx::xxxx:xxxx:xxxx:xxxx%12 xxxx :: xxxx:xxxx:xxxx:xxxx%12

::1 :: 1

in addition to "normal" ones like 除了“正常”的

127.0.0.1 127.0.0.1

192.168.0.3 192.168.0.3

etc 等等

I only want keep these "normal" ones. 我只想保留这些“正常”的。 What criteria (properties, methods) should I use for this? 我应该为此使用什么标准(属性,方法)?

Have a look at using the LINW Where method. 看看使用LINW Where方法。

Something like 就像是

var yourList = NetworkInterface.GetAllNetworkInterfaces().Where(x => /*your boolean expression here>*/)

The 'garbage' ones are IPv6 addresses. “垃圾”是IPv6地址。 They're goodness. 他们是天哪。 But if you only need the IPv4 ones, do: 但是,如果仅需要IPv4,请执行以下操作:

var list = NetworkInterface
    .GetAllNetworkInterfaces()
    .Where(n => n.GetIPProperties().UnicastAddresses.First().Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM