How to determe if a host represents the local host?

  • Thread starter Thread starter Lars Fastrup
  • Start date Start date
L

Lars Fastrup

Hi,

given a host name, how can you i C# determine with 100% certainty if
it actually represents the local host? I am talking about DNS aliases
here.

I have tried the following, which seems to work in most cases. But it
unfortunately fails at a few customer installations:

public bool IsLocalHost(string hostName)
{
IPHostEntry localhost = Dns.GetHostByName(Environment.MachineName);
IPHostEntry hostInfo = Dns.GetHostByName(hostName);
foreach (IPAddress localhostAddress in localhost.AddressList)
{
foreach (IPAddress hostAddress in hostInfo.AddressList)
{
if (localhostAddress.Equals(hostAddress)) return true;
}
}
return false;
}

Any ideas?


Regards
Lars Fastrup
Navigo Systems
www.navigosystems.com
 
Have you tried Dns.GetHostName()? That should get the hostname of the local
computer.

Sujit D'Mello
 
Back
Top