【发布时间】:2011-01-31 19:36:46
【问题描述】:
我只想对以下方法使用 LINQ 语句。我返回字符串列表,但首先我得到 NetworkInterface 列表。
public static List<string> ObtenerDireccionesDeInterfacesDeRedActivos()
{
var listaDirecciones = new List<string>();
var interfacesActivos =
(from networkInterface in NetworkInterface.GetAllNetworkInterfaces()
let
/*IPv4InterfaceStatistics*/
statistics = networkInterface.GetIPv4Statistics()
where
// filter so we see only Internet adapters
networkInterface.OperationalStatus == OperationalStatus.Up
&& networkInterface.NetworkInterfaceType != NetworkInterfaceType.Tunnel
&& networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback
// all testing seems to prove that once an interface comes online
// it has already accrued statistics for both received and sent...
&& (statistics.BytesReceived > 0) && (statistics.BytesSent > 0)
select
networkInterface).ToList<NetworkInterface>();
foreach (NetworkInterface nic in interfacesActivos)
{
var ips = nic.GetIPProperties().UnicastAddresses;
foreach (var ip in ips)
{
listaDirecciones.Add(ip.Address.ToString());
}
}
return listaDirecciones;
}
有什么建议吗?
【问题讨论】: