Query Local IP Address

后端 未结 2 1337
心在旅途
心在旅途 2020-12-07 20:46

I have the need to know my actual local IP address (i.e. not the loopback address) from a Windows 8 WinRT/Metro app. There are several reasons I need this.

相关标签:
2条回答
  • 2020-12-07 21:33

    About the accepted answer, you just need this:

    HostName localHostName = NetworkInformation.GetHostNames().FirstOrDefault(h =>
                        h.IPInformation != null &&
                        h.IPInformation.NetworkAdapter != null);
    

    You can get the local IP Address this way:

    string ipAddress = localHostName.RawName; //XXX.XXX.XXX.XXX
    

    Namespaces used:

    using System.Linq;
    using Windows.Networking;
    using Windows.Networking.Connectivity;
    
    0 讨论(0)
  • 2020-12-07 21:35

    After much digging, 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 string CurrentIPAddress()
    {
        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
                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:

    private IEnumerable<string> GetCurrentIpAddresses()
    {
        var profiles = NetworkInformation.GetConnectionProfiles().ToList();
    
        // the Internet connection profile doesn't seem to be in the above list
        profiles.Add(NetworkInformation.GetInternetConnectionProfile());
    
        IEnumerable<HostName> hostnames =
            NetworkInformation.GetHostNames().Where(h => 
                h.IPInformation != null &&
                h.IPInformation.NetworkAdapter != null).ToList();
    
        return (from h in hostnames
                from p in profiles
                where h.IPInformation.NetworkAdapter.NetworkAdapterId ==
                      p.NetworkAdapter.NetworkAdapterId
                select string.Format("{0}, {1}", p.ProfileName, h.CanonicalName)).ToList();
    }
    
    0 讨论(0)
提交回复
热议问题