【问题标题】:How to get the ip address of a windows 7/8/8.1/10 phone programmatically [duplicate]如何以编程方式获取Windows 7/8/8.1/10手机的IP地址[重复]
【发布时间】:2016-01-22 07:35:56
【问题描述】:

我是这个社区和 Windows 手机应用程序开发的新手。我需要在 Windows Phone 应用程序(C#)上以编程方式获取设备连接到的网络(wifi 和移动)的 IP 地址。需要 Windows 版本 7/8/8.1/10。

【问题讨论】:

  • 你累了吗?
  • @DavidPilkington 我怀疑重复的匹配,因为 OP 专门要求 Win7 向上,而不仅仅是 Win8。
  • 不。对如何进行没有太多想法。看到我的目标是追踪使用我的 webview Url 的用户。我想 ip 地址将是唯一跟踪用户的好方法。

标签: c# windows-phone-7 windows-phone-8 windows-phone-8.1 ip


【解决方案1】:

试试这个:

string ip = "";
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();

IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

IPAddress[] addr = ipEntry.AddressList;

ip = addr[2].ToString();

【讨论】:

    【解决方案2】:

    写一个方法如下

    public static IPAddress GetIpAddress()
    {
    List<string> ipAddresses = new List<string>();
    
    var hostnames = NetworkInformation.GetHostNames();
    foreach (var hn in hostnames)
    {
        if (hn.IPInformation != null)
        {
            string ipAddress = hn.DisplayName;
            ipAddresses.Add(ipAddress);
        }
    }
    
    IPAddress address = IPAddress.Parse(ipAddresses[0]);
    return address;
    }
    

    在需要获取 IP 地址时调用该方法。

    【讨论】:

      猜你喜欢
      • 2014-12-11
      • 2011-10-12
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      • 1970-01-01
      相关资源
      最近更新 更多