问题
I am trying to get the computer name for the current user. I am able to get the IP address using System.Net.Dns.GetHostEntry("ComputerName").Address.ToString()
but when I replace the *ComputerName*with the IPAddress I receive the following error.
No such host is known
I enabled reverse DNS in IIS7 by running command: Cscript.exe adsutil.vbs set w3svc/EnableReverseDNS TRUE in the C:\inetpub\AdminScripts directory on my server.
Any ideas on what I'm doing wrong?
Update
The overall purpose is that this will be a help desk application and it will be useful for a user to be able to easily provide their computer name for assistance.
Locally everything works but it does not work once published to the server.
回答1:
Here is how to obtain it on a different machine. This will also give you the IPv4 version.
class getIP
{
public getIP()
{
IPAddress ip = Dns.GetHostEntry("whybla01").AddressList.Where(o => o.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First();
Console.WriteLine(ip);
string name = Dns.GetHostEntry(ip).HostName.ToString();
Console.WriteLine(name);
}
}
回答2:
You can try this :
string machineName = GetMachineNameFromIPAddress(yourIPAdress);
private static string GetMachineNameFromIPAddress(string ipAdress)
{
string machineName = string.Empty;
try
{
IPHostEntry hostEntry = Dns.GetHostEntry(ipAdress);
machineName = hostEntry.HostName;
}
catch (Exception ex)
{
// Machine not found...
}
return machineName;
}
回答3:
Thanks to all that answered this post.
Actually, the code was not the problem. There was nothing wrong with the code that I was trying or the suggestions made. I found out that the server DNS entry was not set up correctly.
Maybe this will help someone else.
来源:https://stackoverflow.com/questions/24289699/how-to-get-the-computer-name-using-the-ip-address