查询本地IP地址 [英] Query Local IP Address

查看:199
本文介绍了查询本地IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有必要从的Windows 8的WinRT /地铁应用知道我的实际本地IP地址(即不是回送地址)。有几个原因,我需要这个。最简单的是,在应用程序的UI我想表现出像一些文字您的局域网IP地址是:从IP查询$ C $ J]。

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. The simplest is that in the UI of the app I'd like to show some text like "Your local network IP address is: [IP queried from code]".

我们还使用了地址一些额外的网络通讯科。这些通讯科是完全有效的,因为这一切,如果我看在控制面板中的IP地址的作品,然后硬code将其插入应用程序。在一个对话框,要求用户去看看地址,并手动输入它是我真的,​​真的希望避免的。

We also use the address for some additional network comms. Those comms are perfectly valid because it all works if I look at the IP address in the Control Panel, then hard-code it into the app. Asking the user in a dialog to go look at the address and manually enter it is something I really, really want to avoid.

我想这不会是一项复杂的任务编程获取地址,但我的搜索引擎和计算器技能都上来了空。

I would think it wouldn't be a complex task to get the address programmatically, but my search engine and StackOverflow skills are coming up empty.

在这一点上,我开始考虑做一个UDP广播/听环路听到我自己的要求和提取的地址,但真的看起来像一个hackey杂牌。是否有新的WinRT的东西某处的API,将让我去吗?

At this point I'm starting to consider doing a UDP broadcast/listen loop to hear my own request and extract the address from that, but that really seems like a hackey kludge. Is there an API somewhere in the new WinRT stuff that will get me there?

请注意,我说的WinRT的应用程序,这意味着像 Dns.GetHostEntry NetworkInterface.GetAllInterfaces()是行不通的。

Note that I said "WinRT app. That means the typical mechanisms like Dns.GetHostEntry or NetworkInterface.GetAllInterfaces() aren't going to work.

推荐答案

多挖后,我发现您需要使用的信息<一个href=\"http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.connectivity.networkinformation.aspx\">NetworkInformation和<一个href=\"http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.hostname.aspx\">HostName.

After much digging, I found the information you need using NetworkInformation and HostName.

<一个href=\"http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.connectivity.networkinformation.getinternetconnectionprofile.aspx\">NetworkInformation.GetInternetConnectionProfile检索与当前使用本地计算机的网络连接相关联的连接配置文件。

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

<一个href=\"http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.connectivity.networkinformation.gethostnames.aspx\">NetworkInformation.GetHostNames检索主机名的列表。这不是很明显,但是这包括IPv4和IPv6地址字符串。

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

使用该信息,我们可以得到连接到互联网这样的网络适配器的IP地址

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;
}

请注意,<一个href=\"http://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.hostname.aspx\">HostName具有属性CanonicalName,显示名称和RawName,但他们似乎都返回相同的字符串。

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

我们也可以得到类似这样多个适配器与code地址:

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();
}

这篇关于查询本地IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆