Link to home
Start Free TrialLog in
Avatar of pointeman
pointemanFlag for United States of America

asked on

IPHostEntry GetHostEntry IPv6 or IPv4 ?

I've been using the following code to return an IP (v4) from a host name. However, this doesn't work on Vista IPv6 address returns.

Q. How can I change this code to detect the OS and find the IP per Host name accordingly?

        private IPAddress GetIP()
        {
            IPAddress thisIp = null;

            string strHostName = Dns.GetHostName();
            IPHostEntry iphostentry = Dns.GetHostEntry(strHostName);// Find host name

            foreach (IPAddress ipAddress in iphostentry.AddressList)// Grab the first IP addresses
            {
                thisIp = ipAddress;
            }
            return thisIp;
        }

ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pointeman

ASKER

Your code works well.
Q. Will every Vista, Seven, etc O/S return an IPv4 upon  request?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial