如何获取 WindowsStore 应用程序的本地网络 IP 地址 [英] How to get LocalNetwork IP Address For WindowsStore Apps
问题描述
我试图获取本地网络的 IP 地址.为此,我用谷歌搜索并获得了很少的示例,但 WindowsStore 应用程序不支持这些类和方法,这是我的参考链接之一 如何以编程方式获取计算机的本地网络 IP 地址?(C#)
I tried to get the IP Address of the Local Network. For this I googled and getting few samples, but those classes and methods are not supported to WindowsStore Apps, Here is one of my refered link How do I get the Local Network IP address of a computer programmatically? (C#)
如何在 WindowsStore 应用程序中执行此操作?有什么建议吗?
How can I do this in WindowsStore Apps? Any suggestions?
推荐答案
我相信 OP 已经有了他需要的答案,但对于其他人,基于 Roadrunner 的代码:
I believe the OP already has the answer he needs, but for others, based off Roadrunner's code:
public HostName getCurrentIPAddress()
{
IReadOnlyList<HostName> hosts = NetworkInformation.GetHostNames();
foreach (HostName aName in hosts)
{
if (aName.Type == HostNameType.Ipv4)
{
return aName;
}
}
return null;
}
这将获取第一个 IP 地址(这可能是唯一的 IP 地址,至少在我的计算机上)并将其作为主机名返回.要将其作为字符串获取,您始终可以调用 hostname.DisplayName
This gets the first IP address (which is likely the only one, at least on my computer) and returns it as hostname. To get it as a string, you can always call hostname.DisplayName
这篇关于如何获取 WindowsStore 应用程序的本地网络 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!