Click here to Skip to main content
15,920,596 members
Articles / Programming Languages / C#
Tip/Trick

Find IP address in a Metro app (Windows 8)

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
18 Mar 2013CPOL 12.6K   1  
How to find IP address of machine in metro App(Windows 8)

Introduction

How to find the IP address of a machine in a Metro app (Windows 8).

Using the code

DNS is not supported by Metro applications. So to find the IP address in a Metro app implement the following code:

  • Import the Windows.Networking.Connectivity namespace.

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.

C#
 var icp = NetworkInformation.GetInternetConnectionProfile();

if (icp != null && icp.NetworkAdapter != null)
{
    var hostname =
        NetworkInformation.GetHostNames()
            .SingleOrDefault(
                hn =>
                hn.IPInformation != null && hn.IPInformation.NetworkAdapter != null
                && hn.IPInformation.NetworkAdapter.NetworkAdapterId
                == icp.NetworkAdapter.NetworkAdapterId);

    if (hostname != null)
    {
        // the ip address
        txtKeyWord.Text = hostname.CanonicalName;
    }

Hostname has various properties such as Canonical Name, Display Name and Raw Name, but they all seem to return the same string. Here txtKeyWord will give the IP address of the machine (localhost).

License

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


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

Comments and Discussions

 
-- There are no messages in this forum --