我有这个linq方法如何获得所有机器网卡属性,我不想使用linq,我可以帮助转换它而不使用Linq吗?
public NetworkAdapter[] GetAll()
{
return (from adapter in NetworkInterface.GetAllNetworkInterfaces()
from uniCast in adapter.GetIPProperties().UnicastAddresses
where !System.Net.IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6
let lastGatewayAddress = adapter.GetIPProperties().GatewayAddresses.LastOrDefault()
select new NetworkAdapter()
{
string Name = adapter.Name,
string ID = adapter.Id,
string Description = adapter.Description,
string IPAddress = uniCast.Address.ToString(),
string NetworkInterfaceType = adapter.NetworkInterfaceType.ToString(),
string Speed = adapter.Speed.ToString("#,##0"),
string MacAddress = getMacAddress(adapter.GetPhysicalAddress().ToString()),
string gatewayIpAddress = string.Join(" ", adapter.GetIPProperties().GatewayAddresses.Select(a => a.Address))
}).ToArray();
}
这是我试过的;
public void get()
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
Description = adapter.Description;
Name = adapter.Name;
MacAddress = adapter.GetPhysicalAddress().ToString();
IPInterfaceProperties ips = adapter.GetIPProperties();
UnicastIPAddressInformationCollection uniCast = ips.UnicastAddresses;
foreach (UnicastIPAddressInformation ipInfo in uniCast)
{
if (ipInfo.Address.AddressFamily != AddressFamily.InterNetworkV6)
{
}
}
}
}
答案 0 :(得分:5)
虽然我不明白为什么,但这里也是。
在我的朋友ReSharper的帮助下(以及我的意见,因为ReSharper无法完成所有工作):
public NetworkAdapter[] GetAll()
{
List<NetworkAdapter> list = new List<NetworkAdapter>();
foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
foreach (UnicastIPAddressInformation uniCast in adapter.GetIPProperties().UnicastAddresses)
{
if (!System.Net.IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6)
{
StringBuilder gatewayIPAddresses = new StringBuilder();
string gatewayIPAddressesDisplay = string.Empty;
foreach (var address in adapter.GetIPProperties().GatewayAddresses)
{
gatewayIPAddresses.Append(address.Address);
gatewayIPAddresses.Append(" ");
}
if (gatewayIPAddresses.Length > 0)
{
gatewayIPAddressesDisplay = gatewayIPAddresses.ToString().TrimEnd(' ');
}
list.Add(new NetworkAdapter()
{
Name = adapter.Name,
ID = adapter.Id,
Description = adapter.Description,
IPAddress = uniCast.Address.ToString(),
NetworkInterfaceType = adapter.NetworkInterfaceType.ToString(),
Speed = adapter.Speed.ToString("#,##0"),
MacAddress = getMacAddress(adapter.GetPhysicalAddress().ToString()),
gatewayIpAddress = gatewayIPAddressesDisplay
});
}
}
return list.ToArray();
}
答案 1 :(得分:0)
试试这个:
public NetworkAdapter[] GetAll()
{
var networkAdapters = new List<NetworkAdapter>();
foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (UnicastIPAddressInformation uniCast in adapter.GetIPProperties().UnicastAddresses)
{
if (!System.Net.IPAddress.IsLoopback(uniCast.Address) &&
uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6)
{
GatewayIPAddressInformation lastGatewayAddress = adapter.GetIPProperties().GatewayAddresses.LastOrDefault();
networkAdapters.Add(new NetworkAdapter()
{
Name = adapter.Name,
ID = adapter.Id,
Description = adapter.Description,
IPAddress = uniCast.Address.ToString(),
NetworkInterfaceType = adapter.NetworkInterfaceType.ToString(),
Speed = adapter.Speed.ToString("#,##0"),
MacAddress = getMacAddress(adapter.GetPhysicalAddress().ToString()),
gatewayIpAddress = string.Join(" ", adapter.GetIPProperties().GatewayAddresses.Select(a => a.Address))
});
}
}
}
return networkAdapters.ToArray();
}