Click here to Skip to main content
15,903,840 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i am writing a code to find my ip address and i am using this code
C#
private string GetIP()
        {
            string strHostName = "";
            strHostName = System.Net.Dns.GetHostName();

            IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

            IPAddress[] addr = ipEntry.AddressList;

            return addr[addr.Length - 1].ToString();

        }

but it returns me 192.168.1.2 . which but it is not globle ip .
Posted

 
Share this answer
 
Private Function IPAddress() As String
           Dim strIP As String = ""
           Dim LocalHostName As String
           Try
               LocalHostName = Dns.GetHostName()  //Here u will get ur system Name
               Dim ipEnter As IPHostEntry = Dns.GetHostEntry(LocalHostName)
               Dim IpAdd() As IPAddress = ipEnter.AddressList
               strIP = IpAdd(0).ToString()
               Return strIP  //It will return ipaddress of ur system
           Catch ex As Exception
               Throw New Exception(ex.Message)
           End Try
       End Function



With C# CODE-->

C#
private string IPAddress()
{
    string strIP = "";
    string LocalHostName = null;
    try {
        LocalHostName = Dns.GetHostName();
        IPHostEntry ipEnter = Dns.GetHostEntry(LocalHostName);
        IPAddress[] IpAdd = ipEnter.AddressList;
        strIP = IpAdd(0).ToString();
        return strIP;
    } catch (Exception ex) {
        throw new Exception(ex.Message);
    }
}
 
Share this answer
 
v3
Comments
StackQ 13-Dec-12 6:18am    
DON'T forget to add namespace.
StackQ 13-Dec-12 6:32am    
oh sorry it's for local ip address.

u should read this article,It will help u

http://www.codeproject.com/Articles/23673/DNS-NET-Resolver-C

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