How to determe if a host represents the local host?

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
 
G

Guest

Have you tried Dns.GetHostName()? That should get the hostname of the local
computer.

Sujit D'Mello
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top