编辑对象列表
本文关键字:列表 对象 编辑 | 更新日期: 2023-09-27 18:14:15
我有一个对象列表。我的对象是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属性中编辑我的对象,我想将其余的Ip添加到Ip中,而不是192.168.0.100,它变成了192.168.0.100、101、102(此属性为字符串(
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