How do I get the network interface and its right IPv4 address?

后端 未结 3 880
悲哀的现实
悲哀的现实 2020-11-27 14:42

I need to know how to get all network interfaces with their IPv4 address. Or just wireless and Ethernet.

To get all network interfaces details I use this:

3条回答
  •  清酒与你
    2020-11-27 15:24

    One line with Lamda:

    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Sockets;
    using System.Net.NetworkInformation;
    
    var ipV4s = NetworkInterface.GetAllNetworkInterfaces()
        .Select(i => i.GetIPProperties().UnicastAddresses)
        .SelectMany(u => u)
        .Where(u => u.Address.AddressFamily == AddressFamily.InterNetwork)
        .Select(i => i.Address);
    

提交回复
热议问题