I am trying to make an application for deploying to all our workstations at work. I am using VB.NET to create a Windows application. What I need to have done is for the app to open up and display the IP address, MAC address and computer name in text boxes. I need the IP address to be broken down into its 4 octets and each octet populate its own text box (TextBox1-TextBox4).

Anyone have an idea on how to do it?

What I have so far is the IP address part:

Private Sub IPAddress()
'To get local address
Dim LocalHostName As String
Dim i As Integer
LocalHostName = Dns.GetHostName()
Dim ipEnter As IPHostEntry = Dns.GetHostByName(LocalHostName)
Dim IpAdd() As IPAddress = ipEnter.AddressList
For i = 0 To IpAdd.GetUpperBound(0)
Next
End Sub

use System.Management classes to do it. Writing WMI queries. Have a look at it and if you are stuck let me know and i will give you some code.

use System.Management classes to do it. Writing WMI queries. Have a look at it and if you are stuck let me know and i will give you some code.

OK, I have everything working except that I need to split the IP address into its 4 octets and have each octet populate the 4 textboxes. Here is my code for it so far. I have the whole IP in the first textbox:

My variable is:

'To get local IP address
Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
'

And in the area for the text box:

Me.TextBox1.Text = h.AddressList.GetValue(0).ToString

oh ok i thought you were trying to get the MAC and other things too.

your dns is in the form xxx.xxx.xxx.xxx

so there is a clue. it is DELIMITED by the . (period)

so instead of your textbox1.text = h.AddressList.GetValue(0).ToString

dim ipfull as string = h.AddressList.GetValue(0).ToString

dim ipsplit as string[] = ipfull.split(".".tochararray[])

you now have an array in ipsplit.
textbox1.text = ipsplit[0]
textbox2.text = ipsplit[1]
textbox3.text = ipsplit[2]
textbox4.text = ipsplit[3]
all done

oh ok i thought you were trying to get the MAC and other things too.

your dns is in the form xxx.xxx.xxx.xxx

so there is a clue. it is DELIMITED by the . (period)

so instead of your textbox1.text = h.AddressList.GetValue(0).ToString

dim ipfull as string = h.AddressList.GetValue(0).ToString

dim ipsplit as string[] = ipfull.split(".".tochararray[])

you now have an array in ipsplit.
textbox1.text = ipsplit[0]
textbox2.text = ipsplit[1]
textbox3.text = ipsplit[2]
textbox4.text = ipsplit[3]
all done

WORKS GREAT!!! Thanks a million. I need to get some error checking in there now. If the computer is not hooked up or if the LAN port is dead, I need to know it doesn't have a connection. How can I make it do that without erroring out?

Well crud!!

I tested the app on another machine that has a 1394 adapter as the first network adapter, and the app pulled THAT MAC. How do I make it pull the active MAC that the IP address is working on and how do I make my text box display just that MAC?

Here is the current code for the MAC :


'To get MAC Address
Dim mc As System.Management.ManagementClass
Dim mo As ManagementObject
mc = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As ManagementObjectCollection = mc.GetInstances()
For Each mo In moc
If mo.Item("IPEnabled") = True Then
End If
Next

And the current relevant code for the text box:

Me.TextBox5.Text = mo.Item("MacAddress").ToString()

I know something goes in the If Then statement, but everything I tried caused some type of error.

OK, I figured it out!! Yay me!!

'To get MAC Address
Dim mc As System.Management.ManagementClass
Dim mo As ManagementObject
mc = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As ManagementObjectCollection = mc.GetInstances()
Dim mac as string
For Each mo In moc
If mo.Item("IPEnabled") = True Then
mo.Item("MacAddress").ToString
End If
Next

And the current relevant code for the text box:

Me.TextBox5.Text = mac

HttpContext.Current.Request.UserHostAddres

this will give u the address

Edited 6 Years Ago by nilaavu: n/a

hi frnd.....
where i write this code to display an ip address
in a textbox...

when i write on this code on forms load event.....

'To get local IP address
Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)

Me.TextBox1.Text = h.AddressList.GetValue(0).ToString

the code is working, but one warning is generated i.e.

Warning 'Public Shared Function GetHostByName(hostName As String) As System.Net.IPHostEntry' is obsolete: 'GetHostByName is obsoleted for this type, please use GetHostEntry instead.


pls help me...


and im mac address 4 errors are generated.....


i want to display ip address and mac address in textboxes when my project is run.


kindly help me.......

This question has already been answered. Start a new discussion instead.