Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

1 solution

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:
C#
public static string CurrentIPAddress()
{
    var icp = NetworkInformation.GetInternetConnectionProfile();

    if(icp != null && icp.NetworkAdapter != null)
    {
        var hostname =
            NetworkInformation.GetHostNames().SingleOrDefault(
                hn =>
                hn.NetworkAdapter != null &&
                hn.NetworkAdapter.NetworkAdapterId == 
                   icp.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 = 
    NetworkInformation.GetConnectionProfiles().Where(
        p => p.NetworkAdapter != null).ToList();

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

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

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

Wired Ethernet Connection, 192.168.1.71
My SSID, 192.168.1.72
 
Share this answer
 
v2
Comments
vivek.khatri 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.. :)
fjdiewornncalwe 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.khatri 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900