[英]How to get LocalNetwork IP Address For WindowsStore Apps
I tried to get the IP Address of the Local Network. 我试图获取本地网络的IP地址。 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?
为此,我搜索了一些示例,但WindowsStore Apps不支持这些类和方法,这是我引用的链接之一。 如何以编程方式获取计算机的本地IP地址? (C#)
(C#)
How can I do this in WindowsStore Apps? 如何在WindowsStore Apps中执行此操作? Any suggestions?
有什么建议么?
I believe the OP already has the answer he needs, but for others, based off Roadrunner's code: 我相信OP已经有了他需要的答案,但对于其他人,则基于Roadrunner的代码:
public HostName getCurrentIPAddress()
{
IReadOnlyList<HostName> hosts = NetworkInformation.GetHostNames();
foreach (HostName aName in hosts)
{
if (aName.Type == HostNameType.Ipv4)
{
return aName;
}
}
return null;
}
This gets the first IP address (which is likely the only one, at least on my computer) and returns it as hostname. 这将获取第一个IP地址(至少在我的计算机上可能是唯一的IP地址),并将其作为主机名返回。 To get it as a string, you can always call
hostname.DisplayName
要以字符串形式获取它,您始终可以调用
hostname.DisplayName
Dns.GetHostAddresses still exists in 4.5 Dns.GetHostAddresses仍存在于4.5中
http://msdn.microsoft.com/en-us/library/system.net.dns.gethostaddresses.aspx http://msdn.microsoft.com/en-us/library/system.net.dns.gethostaddresses.aspx
I stucked at the same Point when I recently started to Play around with VS2013 and my first store app. 最近开始使用VS2013和我的第一个商店应用程序时,我遇到了同样的问题。 I have not much experience in C#, but I hope what I found out for VB may help you as well.
我没有C#的丰富经验,但是我希望我对VB的发现也能对您有所帮助。 Youre right, the examples with "System.Net.DNS" don't work for Store-Apps.
没错,带有“ System.Net.DNS”的示例不适用于Store-Apps。 But after some researches I found the class "Windows.Networking.Connectivity.NetworkInformation.GetHostNames" that can be used to get not only the Host Name but also the IPv4 and v6 addersses.
但是经过一些研究,我发现了“ Windows.Networking.Connectivity.NetworkInformation.GetHostNames”类,该类不仅可以用来获取主机名,还可以用来获取IPv4和v6加法器。 The following code works fine in Visual Basic
下面的代码在Visual Basic中可以正常工作
HostNamesObj = Windows.Networking.Connectivity.NetworkInformation.GetHostNames
For Each HostName In HostNamesObj
If HostName.Type = 0 Then
ComputerNames= HostName.ToString
End If
If HostName.Type = 1 Then
IPv4_Output = HostName.ToString
End If
If HostName.Type = 2 Then
IPv6_Output = HostName.ToString
End If
Next
If you succeed to translate this to C#, I would be very happy about your Feedback, because that's the Point where I'm strugling right now. 如果您成功将其转换为C#,我将非常高兴收到您的反馈,因为这就是我现在正在努力的重点。 I just realized that C# has a lot in common with VB, but it's much more sensitive regarding the type declaration for variables!
我刚刚意识到C#与VB有很多共同点,但是它对于变量的类型声明要敏感得多! So: Good luck, RoadrunnerLI
所以:祝您好运,RoadrunnerLI
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.