Click here to Skip to main content
15,889,865 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
How to get the local Ip address of system,
actually I have used
C#
public static string LocalIPAddress()
       {
           IPHostEntry host;
           string localIP = "";
           host = Dns.GetHostEntry(Dns.GetHostName());
           foreach (IPAddress ip in host.AddressList)
           {
               if (ip.AddressFamily.ToString() == "InterNetwork")
               {
                   localIP = ip.ToString();
               }
           }
           return localIP;
       }


but when i am publishing the code it always returned the published system's IP.
any help will be apprecited !
Regards
pawan Sharma
Posted
Comments
Sergey Alexandrovich Kryukov 20-Aug-12 2:54am    
Not clear. IP of what? In what scenario?
--SA

Try this:
C#
public String getIpAddress()
   {
       string strIpAddress;
       strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
       if (strIpAddress == null)
       {
           strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
       }
       return Convert.ToString(strIpAddress);
   }
 
Share this answer
 
Comments
Mukund Thakker 20-Aug-12 3:04am    
gives ::1 in local and different ip on hosting server.
codeBegin 20-Aug-12 3:08am    
This code will work when you publish your application.

..and as you have commented different ip on hosting server means its working.
This will return the IP address of whatever system it runs on. Which computer's IP are you looking for?
 
Share this answer
 
Comments
Mukund Thakker 20-Aug-12 3:02am    
this gives ip address of hosted server.
dan!sh 20-Aug-12 5:42am    
Which IP do you want then?
You can get local ip address using sql using following script.

Execute below script , it will return ip address of client system.

SQL
DECLARE @IP_Address varchar(255)
SELECT @IP_Address = client_net_address FROM sys.dm_exec_connections
WHERE Session_id = @@SPID

print(@IP_Address)
 
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