Click here to Skip to main content
15,867,453 members
Articles / Mobile Apps / Windows Phone 7
Tip/Trick

How to get the IP address in Modern UI applications

Rate me:
Please Sign up or sign in to vote.
4.64/5 (11 votes)
29 Apr 2014CPOL 28.2K   20   9
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)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Carsten V2.029-Apr-14 23:20
Carsten V2.029-Apr-14 23:20 
GeneralRe: My vote of 5 Pin
Igor Vigdorchik1-May-14 17:15
Igor Vigdorchik1-May-14 17:15 
GeneralMy vote of 5 Pin
Volynsky Alex29-Apr-14 23:02
professionalVolynsky Alex29-Apr-14 23:02 
GeneralRe: My vote of 5 Pin
Igor Vigdorchik1-May-14 17:15
Igor Vigdorchik1-May-14 17:15 
GeneralRe: My vote of 5 Pin
Volynsky Alex1-May-14 22:51
professionalVolynsky Alex1-May-14 22:51 
GeneralRe: My vote of 5 Pin
Igor Vigdorchik2-May-14 2:35
Igor Vigdorchik2-May-14 2:35 
GeneralRe: My vote of 5 Pin
Volynsky Alex2-May-14 12:08
professionalVolynsky Alex2-May-14 12:08 
SuggestionA little bit short for an article Pin
phil.o29-Apr-14 22:45
professionalphil.o29-Apr-14 22:45 
GeneralRe: A little bit short for an article Pin
Igor Vigdorchik1-May-14 16:05
Igor Vigdorchik1-May-14 16:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.