How to resolve hostname from local IP in C#.NET?
.net, c#
Solution
You can use Dns.GetHostEntry to try to resolve the name, because not every IP has a name.
using System.Net;
...
public string GetHostName(string ipAddress)
{
try
{
IPHostEntry entry = Dns.GetHostEntry(ipAddress);
if (entry != null)
{
return entry.HostName;
}
}
catch (SocketException ex)
{
//unknown host or
//not every IP has a name
//log exception (manage it)
}
return null;
}
Problem
I'm trying to list the names of the computer names currently online on a network. I've only managed to get the get active IPs but I cannot get the computer name of these IPs. Any ideas ?