我有一个对象列表。我的对象是NetworkAdapter
代表我机器上的网卡。此网卡具有属性 ID,例如:{1106B232-363f-417F-9DC9-643BB02BEDE2}
当然还有 Ip 地址、名称等。我的问题是,如果我的网卡有超过 1 个 Ip 地址,那么我的应用程序会显示同一个网卡两次(每次使用不同的ip) 但是因为这是同一张网络卡所以 ID 是相同的所以我想要做的是拿这个List<NetworkAdapter>
并删除具有相同 ID 的重复网卡。
这是我在机器上获取所有适配器的功能:
public static 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(getDevice(adapter.Id))
{
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();
}
最好的方法是什么?