Ip address - Totally new

markm

Newcomer
Joined
Sep 22, 2003
Messages
2
Hi All,

Totally new to .net and trying to find the best way to display the IP address of the local machine and display to a label.

Search on the net shows so many different ways, any suggestions as the best practice?

Thanks in advance.

Mark
 
The .Net DNS class can be used to get a host name or an IP of a given host name. To use DNS class in your project, you need to include System.Net

Include System.Net Reference

And say I want to get IP address if http://www.xtremedotnettalk.com/. The given code will do that for you.




Visual Basic:
Imports System
Imports System.Net

Namespace DNSName

' <summary>
' Summary description for Class1.
' </summary>
Class Class1

'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
Main(System.Environment.GetCommandLineArgs())
End Sub

Overloads Shared Sub Main(args() As String)
Dim ipEntry As IPHostEntry = Dns.GetHostByName("http://www.xtremedotnettalk.com/")
Dim IpAddr As IPAddress() = ipEntry.AddressList
Dim i As Integer
For i = 0 To IpAddr.Length - 1
Console.WriteLine("IP Address {0}: {1} ", i, IpAddr(i).ToString())
Next i
End Sub 'Main
End Class 'Class1
End Namespace 'DNSName
 
if you want the ip of your local machine, all you need is to do this...
Visual Basic:
        Dim ip As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName())
        MessageBox.Show(ip.AddressList(0).ToString)
hope it helps.
 
Back
Top