Click here to Skip to main content
13,501,050 members
Rate this:
Please Sign up or sign in to vote.
I want to find the local ip address of the machine and simply show it in the textbox.
I got lots of snippets in .Net 4.0 OR formal VB.Net, but i am not able to put it in windows 8 metro app. because some of the namespaces are moved or renamed in .Net Framework 4.5..

M using Visual Basic (VB) with XAML for designing in Visual Studio 11 (2012) running on Windows 8 Pro.

Thanks in advance...
Posted 29-Nov-12 8:18am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This[^] answer should help you along your way in Win8.
The gist of the answer is this:
I found the information you need using NetworkInformation and HostName.

NetworkInformation.GetInternetConnectionProfile retrieves the connection profile associated with the internet connection currently used by the local machine.

NetworkInformation.GetHostNames retrieves a list of host names. It's not obvious but this includes IPv4 and IPv6 addresses as strings.

Using this information we can get the IP address of the network adapter connected to the internet like this:
public static string CurrentIPAddress()
    var icp = NetworkInformation.GetInternetConnectionProfile();

    if(icp != null && icp.NetworkAdapter != null)
        var hostname =
                hn =>
                hn.NetworkAdapter != null &&
                hn.NetworkAdapter.NetworkAdapterId == 

        if(hostname != null)
            // the ip address
            return hostname.CanonicalName;

    return string.Empty;
Note that HostName has properties CanonicalName, DisplayName and RawName, but they all seem to return the same string.

We can also get addresses for multiple adapters with code similar to this:

IEnumerable<connectionprofile> profiles = 
        p => p.NetworkAdapter != null).ToList();

IEnumerable<hostname> hostnames = 
    NetworkInformation.GetHostNames().Where(h=>h.NetworkAdapter != null).ToList();

foreach (ConnectionProfile profile in profiles)
        "{0}, {1}",
        hostnames.Single(hn => 
            hn.NetworkAdapter.NetworkAdapterId == 

If I connect a wireless network as well as my wired network then on my machine I get:

Wired Ethernet Connection,
Vivek-The Geek 29-Nov-12 15:05pm
thank you so much for the fast reply...

if i'm not wrong, this will work only if i am connected to the internet, right ???

if i want to know my local IP address then ??

sorry i'm not much familiar to the networking, so pardon if my question is silly.. :)
Marcus Kramer 29-Nov-12 15:23pm
IP addresses are used for any network, either WAN or LAN. The code here will query your network adapter(s) for its(their) IP address(es).
If you are connected to a Local Area Network, then the IP address you read from your adapter will be your local IP address. If you connect directly to the internet via modem or router, then your IP address will likely be obtained from a DNS server "out there" somewhere.
Vivek-The Geek 29-Nov-12 15:40pm
thank you so much for clearing my concept.... :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180417.1 | Last Updated 29 Nov 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100