Get/Display IP Address of my Computer

I want an Button to form that will display my computer's IP address,, how can I do this? Thanks in advance.
Rowel VirgoVisual Studio .NETAsked:
Who is Participating?
Dr. KlahnPrincipal Software EngineerCommented:
Here's an article on how to obtain the IP address in VB and return it with a button.

See the commentary that begins "Shows the Computer Name, Use a Button to call it"
Dr. KlahnPrincipal Software EngineerCommented:
Do you want the IP address of your computer on the local network, or the IP address of the internet-facing router?  The two are not the same and the second is more difficult to obtain.
Rowel VirgoVisual Studio .NETAuthor Commented:
Of my computer sir.,
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Shaun VermaakTechnical Specialist/DeveloperCommented:
You can use this in the button
Dim strComputerName As String
Dim strIPAddress As String

strComputerName = System.Net.Dns.GetHostName()
strIPAddress = System.Net.Dns.GetHostByName(strComputerName).AddressList(0).ToString()

Open in new window

I have personally never liked the heavy handed approach of querying a remote entity for my local information (especially if you may not have access to said entity).  To that end, I prefer to use something based on GetIPProperties instead:
Imports System.Net.NetworkInformation
Imports System.Net.Sockets

Module Module1
	Sub Main()
		For Each adapter In NetworkInterface.GetAllNetworkInterfaces()
			Dim addresses = adapter.GetIPProperties().UnicastAddresses
			Dim gateways = adapter.GetIPProperties().GatewayAddresses
			Dim dns = adapter.GetIPProperties().DnsAddresses
			Console.WriteLine($"Adapter: {adapter.Description}")
			Console.WriteLine($"IPv6 Address: {String.Join(", ", From address In addresses Where address.Address.AddressFamily = AddressFamily.InterNetworkV6 Select address.Address)}")
			Console.WriteLine($"IPv4 Address: {String.Join(", ", From address In addresses Where address.Address.AddressFamily = AddressFamily.InterNetwork Select address.Address)}")
			Console.WriteLine($"Subnet Mask: {String.Join(", ", From address In addresses Where address.Address.AddressFamily = AddressFamily.InterNetwork Select address.IPv4Mask)}")
			Console.WriteLine($"Default Gateway: {String.Join(", ", From gateway In gateways Select gateway.Address)}")
			Console.WriteLine($"DNS: {String.Join(", ", From address In dns Select address)}")
	End Sub
End Module

Open in new window

Which produces the following output -Capture.PNG
Rowel VirgoVisual Studio .NETAuthor Commented:
I did this:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub


    Private Sub GetIPAddr()
        Dim hostName As String
        Dim ipAddr() As IPAddress

        hostName = System.Net.Dns.GetHostName()
        ipAddr = Dns.GetHostAddresses(hostName)

        For i = 1 To ipAddr.Count - 1
            Label4.Text = ipAddr(i).ToString
    End Sub
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.