using system.net namespace.
From IPHostEntry class, we get the list in a string array.
using System.Net; 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(); }Read IP address of the local computer.
You can check your IP address on myip.help.
This is perfect. Thanks.
ReplyDeleteThank you very much...
ReplyDeleteThere is a slight modification in the code if you want it to work in Visual Studio 2010. Just change the "Length-1" to "Length-2".
ReplyDeleteRegards,
Naveen.
Actually -anything is WRONG. Each address in the list corresponds to the protocol supported. Previously, only IPv4 was supported in windows, so [0], the first element in the array, was the right one. Once IPv6 came out and was running in parallel with IPv4, the function returns two elements in the array, and the first happens to now be the IPv6 address. You can identify which is which by the IP family which is one of the members of the returned item. Just hardcoding an index may work, but it can easily fail; as it did when IPv6 was introduced and coutless programs tried to use [0] and got a six part IPv6 address instead of the four part IPv4.
ReplyDeletethis is excellent. GoodStuff....
ReplyDeleteThanks,
Srikanth G
thnks..........
ReplyDeletethank you
ReplyDeletethanks this code is perfect.
ReplyDeletethanks this code is perfect
ReplyDeleteThis is a Perfect code to get the IP Address of the Current machine.
ReplyDeleteThanks a lot.
Regards,
Raaghav
raaghav.nayak86@gmail.com
just not! failsafe - looking for a better solution
ReplyDeleteThanks a lot.......
ReplyDeleteIts very helpful
Thanks n regards
Yashashree
jees...listen to smart lads, the guy at 12/10/2010 1:16 PM is right: this code is awfully wrong. You must specify the protocol. In my case this thing returned 4 elements in a list and the correct one for me was (IPv4) at [2]
ReplyDelete[Length - 2] does not work if your computer have two network adapter
ReplyDeletevar strHostName = Dns.GetHostName();
var ipEntry = Dns.GetHostEntry(strHostName);
var addr = ipEntry.AddressList;
var q = from a in addr
where a.AddressFamily == AddressFamily.InterNetwork
select a;
return q.Last().ToString();
System.Web.HttpContext.Current.Request.UserHostAddress;
ReplyDeletewhy we use q.Last().ToString().why not q.firstordefault. Any specila purpose is there..
ReplyDelete@Sophie, I think your code snippet attempts to return the IP address of the browsing user, rather than the machine the app is running on.
ReplyDeleteits helpful
ReplyDeleteIn vs 2010 Ipaddress in found in length-3
ReplyDeleteIn vs 2010 if u r connected to internet than u will get ur IPADDRESS with ipaddr.length-3 and if u not connected than u will get by ipaddr.length-1
ReplyDeleteThanksss
ReplyDeleteUnexpectedly I have come back to this blog post after 4 years :). Wow..!
ReplyDeleteWhat gucs has proposed is partially ok, but cannot be regarded as a generic solution. In my case, all are ok except the last line where I have to get the first one to get the actual local ipaddress. I have two adapters which are up, and my Local Area Network happenned to be the first one in the list.
var strHostName = Dns.GetHostName();
var ipEntry = Dns.GetHostEntry(strHostName);
var addr = ipEntry.AddressList;
var q = from a in addr
where a.AddressFamily == AddressFamily.InterNetwork
select a;
return q.First().ToString();
System.Net.NetworkInformation might have a solution if wired up with this code of gucs or ASG, to get exactly the intended Ipaddress we would need.
I'll get back if I found a way.
private string GetLocalIp()
ReplyDelete{
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
return ip.ToString();
}
return "127.0.0.1";
}