Click here to Skip to main content
16,020,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to retrieve the local IP address on my system

Some of the article in google shows some method,but its not working.

Please help me to do it correctly


The way i had used :

Request.Servervariables("REMOTE_ADDR")
Request.Servervariables("LOCAL_ADDR")


etc,but all of it is giving 127.0.0.1

Its not correct. Kindly help


Kishore
Posted
Updated 23-Jan-11 20:23pm
v2

If you are using development server the default IP of your machine will be 127.0.0.1
Publish your application in IIS then you can get the IP adress of your machine using Request.Servervariables("REMOTE_ADDR")
 
Share this answer
 
hi
Request.UserHostAddress;

I hope this may help you..
 
Share this answer
 
Try this code :

To get a host with a www address
IPHostEntry ip = Dns.GetHostByName ("www.vbcity.com"); 
IPAddress [] IpA = ipE.AddressList; 
for (int i = 0; i < IpA.Length; i++) 
{ 
    Console.WriteLine ("IP Address {0}: {1} ", i, IpA[i].ToString ()); 
} 


To get the local IP address
string sHostName = Dns.GetHostName (); 
IPHostEntry ipE = Dns.GetHostByName (sHostName); 
IPAddress [] IpA = ipE.AddressList; 
for (int i = 0; i < IpA.Length; i++) 
{ 
    Console.WriteLine ("IP Address {0}: {1} ", i, IpA[i].ToString ()); 
}
 
Share this answer
 
v2
try this ->
C#
string ip = Request.Params["REMOTE_ADDR"].ToString();

when you run the project in locally then you will get 127.0.0.1 but when you run it in the server, it will give the currect ip address.
 
Share this answer
 
v3
You can also use WMI
WIN32_NetworkAdapterConfiguration
using system.Management
C#
using System;
using System.Management;

public class Sample
{
    public static void Main(string[] args)
    {
        ManagementObjectSearcher s =
            new ManagementObjectSearcher(
                "SELECT * FROM Win32_NetworkAdapterConfiguration");

        foreach (ManagementObject service in s.Get())
        {
            // show the instance
            Console.WriteLine(service.ToString());
           // u can get IP address from this array
        }
    }

}
 
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