Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As mention in the subject,
i was trying to get the host IP address,
the code run perfectly , and i got an unexpected results...
Instead of IP address in IPv4 format , i got it in IPv6 format!

How shall i overcome this issue?

C#
try
    {
        //iphostname = Dns.GetHostName();  // Resolving Host name
        IPHostEntry ipentry = Dns.GetHostEntry(hostLabel.Text);
        IPAddress[] addr = ipentry.AddressList;// Resolving IP Addresses
        for (int i = 0; i < addr.Length; i++)
        {
            try
            {
                ipLabel.Text=  Convert.ToString(addr[i]) + "\r\n";
            }
            catch
            {
                ipLabel.Text += "IP Address            | " + "\r\n";
            }
        }
    }
    catch
    {
                  | " + "\r\n";
    }
}

Screenshot reference : https://lh6.googleusercontent.com/-vn4v728bOQI/UCNu_rlz6II/AAAAAAAAANI/UYbtgxNqt-0/s616/Fullscreen+capture+982012+40215+PM.jpg[^]
Posted
Updated 8-Aug-12 22:10pm
v2

 
Share this answer
 
string strHostName = "";
 strHostName = System.Net.Dns.GetHostName();
 IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
 IPAddress[] addr = ipEntry.AddressList;
 ipLabel.Text = addr[addr.Length - 2].ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900