65.9K
CodeProject is changing. Read more.
Home

How to get the IP address in Modern UI applications

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.64/5 (11 votes)

Dec 23, 2012

CPOL
viewsIcon

29605

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.

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

string ip = GetIPAddress();