4

A particular rest service I am calling in a Windows Store app wants the IP address of the calling computer as a parameter. None of the tried and true examples using System.Net or System.Net.NetworkInformation compile in a Store app.

What is the magical combination of types and methods available in a windows store app that will get to the local machine's IP address? I feel like it is probably obvious but I am not seeing it!

Community
  • 1
  • 1
dkackman
  • 15,179
  • 13
  • 69
  • 123

2 Answers2

1

You will have to contact an external server. Even if the platform supplies an API to retrieve the network address, the host could still be located behind a proxy or a NAT (and you will see something like 192.168.1.4, instead of your external IP address).

Just perform an HTTP request towards services like http://ifconfig.me/ or http://whatismyip.com/ and parse the IP.

Andrea
  • 724
  • 1
  • 8
  • 12
1

Local network IP address:

foreach (HostName localHostName in NetworkInformation.GetHostNames())
{
    if (localHostName.IPInformation != null)
    {
        if (localHostName.Type == HostNameType.Ipv4)
        {
            // E.g.: 192.168.1.108
            Debug.WriteLine(localHostName);
        }
    }
}
kiewic
  • 15,852
  • 13
  • 78
  • 101