【问题标题】:Finding all available IPs in Windows Phone 8?在 Windows Phone 8 中查找所有可用的 IP?
【发布时间】:2013-12-01 22:14:36
【问题描述】:

我正在尝试找到一种方法来查看任何一台设备上的所有可用 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]);
    }

谢谢

【问题讨论】:

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


    【解决方案1】:

    你可以在System.Net.NetworkInformation命名空间中使用NetworkInterface.GetAllNetworkInterfaces()

    List<IPAddress> ips = NetworkInterface.GetAllNetworkInterfaces()
                  .Where(x => x.NetworkInterfaceType != NetworkInterfaceType.Loopback)
                  .SelectMany(x => x.GetIPProperties().UnicastAddresses)
                  .Select(x => x.Address)
                  .ToList();
    

    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();
    

    【讨论】:

    • 谢谢 LB,我会试试看的!任何想法我将如何用结果填充一个数组?我可以使用 LongListViewer 来显示它们吗?
    • GeetAllNetworkInterfaces() 不适用于 Windows 应用商店应用。有什么解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多