How can i get ip address of system by sending mac ip address as input using vb.net coding?
11 Answers
Use the My Class :)
My.Computer.Name
as for the IP address quick google search
Private Sub GetIPAddress()
Dim strHostName As String
Dim strIPAddress As String
strHostName = System.Net.Dns.GetHostName()
strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()
MessageBox.Show("Host Name: " & strHostName & "; IP Address: " & strIPAddress)
End Sub
-
If you are using VB.Net 2.0 or better you should be able to use it out of the box :)– ShuwaieeFeb 10, 2010 at 6:13
-
8Since
GetHostByName()
is deprecated and gives you a warning in vb.net, you might wanna simply replace it withGetHostEntry()
which does the same, but makes the compiler happy. Eg:strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()
– LeviteMay 23, 2014 at 8:13 -
1Also obviously this gives you only the first IP, if you want to loop through all addresses it is quite easy as well:
For Each adr In System.Net.Dns.GetHostEntry(strHostName).AddressList()
and in the loop you simply check/print/whateveradr.ToString()
– LeviteMay 23, 2014 at 8:16 -
2This anwser won't work for everyone, it works sometimes because there is only 1 item in the AddressList. When there are many, most often the first few are not IPV4 addresses. The right way would be GetIPv4Address() from EagleEye's answer. The loop will find the IPv4 address wherever it is in the list. Jun 9, 2016 at 20:34
Private Function GetIPv4Address() As String
GetIPv4Address = String.Empty
Dim strHostName As String = System.Net.Dns.GetHostName()
Dim iphe As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(strHostName)
For Each ipheal As System.Net.IPAddress In iphe.AddressList
If ipheal.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
GetIPv4Address = ipheal.ToString()
End If
Next
End Function
-
3This is the right way to get the IPv4 address. In my log here there are 4 different strings in the AddressList and 3 of them are useless. You have to loop and compare them all to find which one is the IPv4. Jun 9, 2016 at 20:35
-
1
-
If someone is on multiple networks (e.g. a LAN and a VPN) they will have multiple IPv4Address. If you're not sure which one you'll want, best to get all of them and let the caller decide which one(s) they need.– montyMay 25, 2018 at 0:01
Here is Example for this. In this example we can get IP address of our given host name.
Dim strHostName As String = "jayeshsorathia.blogspot.com"
'string strHostName = "www.microsoft.com";
' Get DNS entry of specified host name
Dim addresses As IPAddress() = Dns.GetHostEntry(strHostName).AddressList
' The DNS entry may contains more than one IP addresses.
' Iterate them and display each along with the type of address (AddressFamily).
For Each address As IPAddress In addresses
Response.Write(String.Format("{0} = {1} ({2})", strHostName, address, address.AddressFamily))
Response.Write("<br/><br/>")
Next
Shows the Computer Name, Use a Button to call it
Dim strHostName As String
strHostName = System.Net.Dns.GetHostName(). MsgBox(strHostName)
Shows the User Name, Use a Button to call it
If TypeOf My.User.CurrentPrincipal Is Security.Principal.WindowsPrincipal Then
Dim parts() As String = Split(My.User.Name, "\") Dim username As String = parts(1) MsgBox(username) End If
For IP Address its little complicated, But I try to explain as much as I can. First write the next code, before Form1_Load but after import section
Public Class Form1
Dim mem As String Private Sub GetIPAddress() Dim strHostName As String Dim strIPAddress As String strHostName = System.Net.Dns.GetHostName() strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString() mem = strIPAddress MessageBox.Show("IP Address: " & strIPAddress) End Sub
Then in Form1_Load Section just call it
GetIPAddress()
Result: On form load it will show a msgbox along with the IP address, for put into Label1.text or some where else play with the code.
Thanks Shuwaiee
I made a slight change though as using it in a Private Sub
already.
Dim GetIPAddress()
Dim strHostName As String
Dim strIPAddress As String
strHostName = System.Net.Dns.GetHostName()
strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()
MessageBox.Show("Host Name: " & strHostName & vbCrLf & "IP Address: " & strIPAddress)
But also made a change to the way the details are displayed so that they can show on seperate lines using & vbCrLf &
MessageBox.Show("Host Name: " & strHostName & vbCrLf & "IP Address: " & strIPAddress)
Hope this helps someone.
Dim ipAddress As IPAddress
Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
ipAddress = ipHostInfo.AddressList(0)
Imports System.Net
Module MainLine
Sub Main()
Dim hostName As String = Dns.GetHostName
Console.WriteLine("Host Name : " & hostName & vbNewLine)
For Each address In Dns.GetHostEntry(hostName).AddressList()
Select Case Convert.ToInt32(address.AddressFamily)
Case 2
Console.WriteLine("IP Version 4 Address: " & address.ToString)
Case 23
Console.WriteLine("IP Version 6 Address: " & address.ToString)
End Select
Next
Console.ReadKey()
End Sub
End Module
IP Version 4 Only ...
Imports System.Net
Module MainLine
Sub Main()
Dim hostName As String = Dns.GetHostName
Console.WriteLine("Host Name: " & hostName & vbNewLine)
Console.WriteLine("IP Version 4 Address(es):")
For Each address In Dns.GetHostEntry(hostName).AddressList().
Where(Function(p) p.AddressFamily = Sockets.AddressFamily.InterNetwork)
Console.WriteLine(vbTab & address.ToString)
Next
Console.ReadKey()
End Sub
End Module
Label12.Text = "My ID : " + System.Net.Dns.GetHostByName(Net.Dns.GetHostName()).AddressList(0).ToString()
use this code, without any variable
In recent versions of MS Visual Studio for VB.NET, one should use the method GetHostEntry()
:
Dim HostName = "N/A"
Dim ipAddress = "N/A"
Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
HostName = Dns.GetHostName()
ipAddress = ipHostInfo.AddressList(1).ToString()
Now, keep in mind that AddressList(0)
is the ::1
network address, which we want to skip and use the second address in the list. Of course, without hardware network card the second element in the list will be absent, so you have to perform additional check in order to avoid ArgumentOutOfRangeException
.
Public strHostName As String
Public strIPAddress As String
strHostName = System.Net.Dns.GetHostName()
strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()
MessageBox.Show("Host Name: " & strHostName & "; IP Address: " & strIPAddress)