How to get the computer name using the IP Address

Stacking Overflows picture Stacking Overflows · Jun 18, 2014 · Viewed 22.7k times · Source

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.

Answer

Moez Rebai picture Moez Rebai · Jun 18, 2014

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;
        }