Reputation: 53
I need to get a list of all the network names that the user is connected to and the IP address of the computer on that network. I can get a list of network names using NetworkInformation.GetConnectionProfiles() and I can get a list of IP addresses using NetworkInformation.GetHostNames(), but I cannot figure out how to associate the two lists. I thought it would be through the NetworkAdapters but the IDs for those are different between the two different calls.
Does anyone know how to do this?
UPDATE: Just to clarify, I'm asking how to do this in a Metro app on Windows 8.
UPDATE 2: It turns out it was a bug in Release Preview. Now that we've switched to RTM everything worked without a single change to the existing code.
Upvotes: 1
Views: 1797
Reputation: 5633
EDIT: Just found this... Query Local IP Address Looks like a cleaner way. Take your pick...
I'm gonna take a stab... hopefully gets you close. I only have one adapter that is connected on my test machine, but I think (hope) this would work in a machine with multiple adapaters.
// Get all profiles
var profiles = NetworkInformation.GetConnectionProfiles();
// filter out profiles that are not currently online
var connected = from p in profiles where p.GetNetworkConnectivityLevel() != NetworkConnectivityLevel.None select p;
// find all hosts
var hosts = NetworkInformation.GetHostNames();
// find hosts that have an IP Address
var online = from h in hosts where h.IPInformation != null select h;
// Now loop there each online connection and match network adapter ids with hosts
foreach (var c in connected)
{
var matches = from o in online where o.IPInformation.NetworkAdapter.NetworkAdapterId == c.NetworkAdapter.NetworkAdapterId select o;
}
The only problem here is that a single physical adapter will actually show up once for its IPv4 address and once for its IPv6 address. You will have to go the extra step and correlate them together. Hopefully this is what you are looking for.
Upvotes: 1
Reputation: 4282
well, I don't know about c# or the .net framework, but from a windows command prompt, the command netstat -f is what you need. Perhaps you could parse that.
Update
I think I understand what you need. If you have a list of IP addresses, you can convert them to hostnames with Dns.GetHostEntry
Have a look on this article, which discusses the issue in C#.
Upvotes: 0