在Windows Phone 8中查找所有可用的IP?

时间:2013-12-01 22:14:36

标签: c# windows-phone-8 ip-address

我正试图找到一种方法来查看任何一台设备上的所有可用IP地址?

这是我用于Android的内容:

public String[] getLocalIpAddress()
    {          
        ArrayList<String> addresses = new ArrayList<String>();
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {    
                        addresses.add(inetAddress.getHostAddress().toString());
                    }
                 }
             }
         } catch (SocketException ex) {
             String LOG_TAG = null;
             Log.e(LOG_TAG, ex.toString());
         }
         return addresses.toArray(new String[0]);
    }

由于

1 个答案:

答案 0 :(得分:2)

您可以在NetworkInterface.GetAllNetworkInterfaces()命名空间

中使用System.Net.NetworkInformation
List<IPAddress> ips = NetworkInterface.GetAllNetworkInterfaces()
              .Where(x => x.NetworkInterfaceType != NetworkInterfaceType.Loopback)
              .SelectMany(x => x.GetIPProperties().UnicastAddresses)
              .Select(x => x.Address)
              .ToList();

OR

var  ips = NetworkInterface.GetAllNetworkInterfaces()
           .Where(inf => inf.NetworkInterfaceType != NetworkInterfaceType.Loopback)
           .Where(inf => inf.OperationalStatus == OperationalStatus.Up)
           .Select(x => new{
                name = x.Name,
                ips = x.GetIPProperties().UnicastAddresses.Select(y=>y.Address)
                      .ToList()
           })
           .ToList();