Yasserzaid’s Weblog

January 16, 2012

Get local computer IP address

Filed under: ASP.Net — yasserzaid @ 8:24 am

Hi all,

try this example to Get local computer IP address we will create a new method which will return IP Address


private string GetLocalIP()
 {
 string _IP = null;
 // Resolves a host name or IP address to an IPHostEntry instance.
 // IPHostEntry - Provides a container class for Internet host address information.
 System.Net.IPHostEntry _IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
 // IPAddress class contains the address of a computer on an IP network.
 foreach (System.Net.IPAddress _IPAddress in _IPHostEntry.AddressList)
 {
 // InterNetwork indicates that an IP version 4 address is expected
 // when a Socket connects to an endpoint
 if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
 {
 _IP = _IPAddress.ToString();
 }
 }
 return _IP;
 }

Hope this helps

Good Luck

Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.