Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Windows-Phone-7

How to get the IP address in Modern UI applications

4.64/5 (11 votes)
29 Apr 2014CPOL 29.5K  
Presents a simple function to get the IPv4 address

This small function shows how to get the IPv4 address in Modern UI applications that are targeting the new Windows Store.

C#
public string GetIPAddress()
{
    // Get a list of host names associated with the local machine
    IReadOnlyList<Windows.Networking.HostName> hostNames = Windows.Networking.Connectivity.NetworkInformation.GetHostNames();

    // The names can be one of four different types: DomainName, Ipv4, Ipv6, Bluetooth
    foreach (Windows.Networking.HostName name in hostNames)
    {
        if (name.Type == Windows.Networking.HostNameType.Ipv4)
            return name.DisplayName;
    }

    return string.Empty; ;
}  

The usage cannot be simpler

C#
string ip = GetIPAddress();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)