4

i have this linq method how get all machine network card properties and i don't want to use linq, can i have some help to convert it and not using 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();
}

this is what i have try;

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)
            {

            }
        }

    }
}
4

2 回答 2

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();
    }
于 2013-04-18T10:22:37.327 回答
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();
        }
于 2013-04-18T10:25:40.253 回答