使用GetHostEntry
函数从IP地址获取主机名
static string GetMachineNameFromIPAddress(string ipAddress)
{
string machineName = null;
try
{
IPHostEntry hostEntry = Dns.GetHostEntry(ipAddress);
machineName = hostEntry.HostName;
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
return machineName;
}
使用PING
命名获取主机名
ping -a 192.168.31.67
通过GetHostAddresses
获取主机IP
static string GetIPAddressFromMachineName(string machineName)
{
string ipAdress = string.Empty;
try
{
IPAddress[] ipAddresses = Dns.GetHostAddresses(machineName);
IPAddress ip = ipAddresses[1];
ipAdress = ip.ToString();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
return ipAdress;
}
网址
https://morgantechspace.com/2013/10/Get-machine-name-from-IP-Address-c-sharp.html