0

我有一个对象列表。我的对象是 NetworkAdapter,它代表我机器上的网卡。此网卡具有属性 ID,例如:{1106B232-363f-417F-9DC9-643BB02BEDE2},当然还有 IP 地址、名称等。

构造函数:

   public NetworkAdapter(LivePacketDevice packetDevice)
        {
            livePacketDevice = packetDevice;
        }

班级成员:

public string Name { get; set; }
public string ID { get; set; }
public string Description { get; set; }
public string IPAddress { get; set; }
private string gatewayIpAddress;
public string Speed { get; set; }
public string NetworkInterfaceType { get; set; }
public string MacAddress { get; set; }
private LivePacketDevice livePacketDevice;
public PacketDevice PacketDevice { get { return livePacketDevice; } }

我获得所有机器适配器的方法:

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.GroupBy(n => n.ID).Select(g => g.FirstOrDefault()).ToArray();
}

在此方法结束时,我在组合所有相同的网卡后返回列表:

return list.GroupBy(n => n.ID).Select(g => g.FirstOrDefault()).ToArray();

所以现在它解决了我的问题,我只能看到每个适配器和我的所有适配器属性通过标签显示在我的表单上。我想做什么我不知道如何从同一个适配器(具有相同的 id)中获取所有 IP 地址并将它们一起显示,例如:

我在同一个适配器上配置了 3 个 IP 地址:192.168.0.100, 192.168.0.101, 192.168.0.102

所以我想在 Ip Address 属性中编辑我的对象,我想将其余的 Ips 添加到 IP,而不是192.168.0.100它变为192.168.0.100、101、102(此属性是字符串)

4

1 回答 1

0
return list.GroupBy(n => n.ID).Select(g => new NetworkAdapter()
        {
            Description = g.First().Description,
            ID = g.Key,
            IPAddress = string.Join(",", g.Select(v => v.IPAddress)),
            MacAddress = g.First().MacAddress,
            Name = g.First().Name,
            NetworkInterfaceType = g.First().NetworkInterfaceType,
            Speed = g.First().Speed
        }).ToList();

192.168.0.100, 192.168.0.101, 192.168.0.102需要处理这个92.168.0.100, 101, 102

于 2013-05-17T17:51:16.583 回答