-3

Possible Duplicate:
How to get local ip address using vb?

I am using Silverlight3 with VB.NET. I have written the follwing code in my service file. When I run my project locally, it gives the IP address of my system as 192.168.1.12 successfully. But, when I host my project it wont return the IP adress of the system which I am using.

ie: If i host my project in system A which is of ip 192.168.1.12 and if I access the project from system B which is of ip 192.168.1.7, the output of my project will gives the IP of system A (where i have hosted my project). It should give the IP of system B where am running it. Similarly, if am accessing the project from system C means it should give the IP of system C, but here it is returning the IP of system A.

Kindly help me please. Thanks

Private Function fnLocalIp() As String
    Try
        Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
        Dim strLocalIp As String = h.AddressList.GetValue(0).ToString
        Return strLocalIp
    Catch ex As Exception
    End Try
End Function
Community
  • 1
  • 1
Rakesh
  • 87
  • 1
  • 3
  • 9

2 Answers2

5

You can get local ip using below code:

in vb.net:

Dim host As String = System.Net.Dns.GetHostName()
Dim LocalHostaddress As String = System.Net.Dns.GetHostByName(host).AddressList(1).ToString()

In c#:

string host = System.Net.Dns.GetHostName();
string LocalHostaddress = System.Net.Dns.GetHostByName(host).AddressList[1].ToString();
Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178
0

use dns Class , more information here

http://msdn.microsoft.com/it-it/library/system.net.dns.aspx

Regards.

  • Thanks but it is not working. it is returning Static ip as 122.166.48.230. i want local ip which is like 192.168.1.12 – Rakesh Aug 27 '11 at 13:45