2

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?

Community
  • 1
  • 1
Lokesh
  • 5,180
  • 4
  • 27
  • 42
  • Which classes and methods do you think are not supported in .NET 4.5? Or are you *actually* talking about a Windows Store app? – Jon Skeet Dec 24 '12 at 08:35
  • Yes.. I was tried in WindowsStore Apps. – Lokesh Dec 24 '12 at 08:37
  • Well that's completely different to .NET 4.5, which is a superset of previous desktop .NET versions. Please edit your question, and be more careful in future. – Jon Skeet Dec 24 '12 at 08:37
  • Thanks Jon, but Is it possible to do for store Apps?? If possible, how can I do that?? – Lokesh Dec 24 '12 at 08:42
  • May be this will help http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/e9c3a808-6b29-4885-964d-0c961e8ac65d/ – Likurg Dec 24 '12 at 08:42
  • @Lokesh: How about you edit your question first, to avoid further confusion? – Jon Skeet Dec 24 '12 at 08:45
  • I edited my question, pleas refer.. – Lokesh Dec 24 '12 at 08:49
  • Right. Now look at Likurg's link - you'll just need to translate that into C#, which shouldn't be too hard. – Jon Skeet Dec 24 '12 at 08:51
  • Thank you both Jon and Likurg for giving helpful tips. Here I founded the sample http://caioproiete.net/en/get-the-network-machine-name-in-a-windows-store-app/ – Lokesh Dec 24 '12 at 09:09

3 Answers3

6

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

ChooJeremy
  • 63
  • 2
  • 7
0

Dns.GetHostAddresses still exists in 4.5

http://msdn.microsoft.com/en-us/library/system.net.dns.gethostaddresses.aspx

Marq Watkin
  • 140
  • 1
  • 1
  • 6
  • Thank you Marq, but I was trying this for windows store apps. Need add more stuff to supporting this?? – Lokesh Dec 24 '12 at 08:39
0

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