Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  How to get IP addresses in .NET with a host name
How to get IP addresses in .NET with a host name
by John Spano | Published  12/01/2002 | .NET Framework | Rating:
John Spano

John Spano cofounder and CTO of NeoTekSystems, a Greenville, South Carolina technology consulting company. NeoTekSystems offers IT consulting, custom programming, web design and web hosting. We specialize in Microsoft .Net enterprise development and business design.

I have six years of experience in software architecture. My primary focus is on Microsoft technologies, and I have been involved in .NET since beta 1. I currently hold a MCSD certification, 2 MCTS's (Windows, Web) a MCPD in Distributed, 2 MCITP's, a Microsoft MVP, and have won the Helper of the Month contest for July 2002 in the devCity.NET forums.

Corporate URL: www.NeoTekSystems.com
Primary email: JSpano@NeoTekSystems.com
Alternate email: Jspano@devcity.net.

 

View all articles by John Spano...
How to get IP addresses in .NET with a host name

To get an IP address from a hostname you can use these functions:

You must first import/using System.Net

C#

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 ());
}


VB.NET

Code:
'To get a www address
Dim i As Integer
Dim ipE As IPHostEntry = Dns.GetHostByName("www.vbcity.com")
Dim IpA() As IPAddress = ipE.AddressList
For i = 0 To IpA.GetUpperBound(0)
    Console.Write("IP Address {0}: {1} ", i, IpA(i).ToString)
Next

'To get local address
Dim sHostName As String
Dim i As Integer
sHostName = Dns.GetHostName()
Dim ipE As IPHostEntry = Dns.GetHostByName(sHostName)
Dim IpA() As IPAddress = ipE.AddressList
For i = 0 To IpA.GetUpperBound(0)
    Console.Write("IP Address {0}: {1} ", i, IpA(i).ToString)
Next

This article was originally posted as devCity.NET Forums FAQ - http://www.devcity.net/forums/faq.asp?fid=30#TID4879

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:4.55263157894738 out of 5
 38 people have rated this page
Article Score33945
Comments    Submit Comment

Comment #1  (Posted by Prashant Khandelwal on 12/30/2002)

Is there any function or method in C# or any other way for that matter, which can be called to get the size of memory of a user's system. I would really appreciate any direction or guidance to do this.
Thank you,
Prashant
 
Comment #2  (Posted by John on 12/30/2002)

Check out the System.Management namespace. There are classes to retrieve many different hardware settings.
 
Comment #3  (Posted by Nadia on 09/05/2003)

I create code to take the computer IP address.
I run the code on different PC.
On one PC I get the IP address, while on the other PC I get 127.0.0.1, on another localhost and on the last I get the PC name.
Is it related to the machine configuration?
I want always the IP address. How can I resolve this problem?
Thanks.
Nadia

 
Comment #4  (Posted by Tancev Sasa on 11/05/2003)

I am interesting how to get MAC addres from LAN card.
I can do thet In VB6, via API, but do know how in .NET.
 
Comment #5  (Posted by an unknown user on 05/06/2005)
Rating
This was the very best answer I found
 
Comment #6  (Posted by an unknown user on 08/09/2005)
Rating
This was a nice, short solution that actually works!
 
Comment #7  (Posted by an unknown user on 11/09/2005)
Rating
this code is useful for my project INTRANET MESSENGER
 
Comment #8  (Posted by madhavan on 02/13/2006)
Rating
i need to find all systems connected in a lan.and if there are multiple domains,then how can i find all domains and list out the systems in each domain..thanks in advance
 
Comment #9  (Posted by an unknown user on 03/22/2006)
Rating
Just what I was looking for

Thanks a Bunch
 
Comment #10  (Posted by an unknown user on 04/25/2006)
Rating
Very easy to understand!
 
Comment #11  (Posted by an unknown user on 07/10/2006)
Rating
This helped me sooooooo much. This is the best snippet ever!
 
Comment #12  (Posted by an unknown user on 07/26/2006)
Rating
How do you determine which is the primary IP Address.
 
Comment #13  (Posted by an unknown user on 11/26/2006)
Rating
The VB was a starting point that got me on the way, but it uses deprecated functions.
 
Comment #14  (Posted by an unknown user on 02/21/2007)
Rating
I got what I was looking for in the simplest way.
 
Comment #15  (Posted by an unknown user on 03/26/2007)
Rating
nice code

 
Comment #16  (Posted by an unknown user on 05/11/2007)
Rating
do i need a live IP for a messener
 
Comment #17  (Posted by suryakant on 05/11/2007)
Rating
hi, i am new in this type of application
i try to develop messenger
do we need a live ip for a develop messenger
 
Comment #18  (Posted by an unknown user on 05/16/2007)
Rating
for declaring variables class info. not there
 
Comment #19  (Posted by an unknown user on 01/26/2008)
Rating
This may appear obvious to vb.net experts, but to us common folks, be sure to put System.Net.Dns.GetHostByName
 
Comment #20  (Posted by an unknown user on 02/18/2008)
Rating
Thank's !!!

 
Comment #21  (Posted by an unknown user on 05/22/2008)
Rating
Good
 
Sponsored Links