如何解决在C#.NET本地IP主机名? [英] How to resolve hostname from local IP in C#.NET?
本文介绍了如何解决在C#.NET本地IP主机名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在线列出一个网络上的计算机名称的名称。我只设法得到积极获取IP地址,但我不能让这些IP的计算机名称。任何想法?
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 ?
在此先感谢!
推荐答案
您可以使用 Dns.GetHostEntry 为尝试来解析名称,因为<强>不是每个IP有一个名字
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;
}
这篇关于如何解决在C#.NET本地IP主机名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文