问题
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#)
How can I do this in WindowsStore Apps? Any suggestions?
回答1:
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;
}
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
回答2:
Dns.GetHostAddresses still exists in 4.5
http://msdn.microsoft.com/en-us/library/system.net.dns.gethostaddresses.aspx
回答3:
I stucked at the same Point when I recently started to Play around with VS2013 and my first store app. I have not much experience in C#, but I hope what I found out for VB may help you as well. Youre right, the examples with "System.Net.DNS" don't work for 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. The following code works fine in 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. I just realized that C# has a lot in common with VB, but it's much more sensitive regarding the type declaration for variables! So: Good luck, RoadrunnerLI
来源:https://stackoverflow.com/questions/14018978/how-to-get-localnetwork-ip-address-for-windowsstore-apps