Get local IP address

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2006
Posts: 6
Reputation: Bill the Cat is an unknown quantity at this point 
Solved Threads: 0
Bill the Cat Bill the Cat is offline Offline
Newbie Poster

Get local IP address

 
0
  #1
Jan 19th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 275
Reputation: f1 fan is an unknown quantity at this point 
Solved Threads: 11
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Re: Get local IP address

 
0
  #2
Jan 21st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 6
Reputation: Bill the Cat is an unknown quantity at this point 
Solved Threads: 0
Bill the Cat Bill the Cat is offline Offline
Newbie Poster

Re: Get local IP address

 
0
  #3
Jan 21st, 2006
Originally Posted by f1 fan
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 275
Reputation: f1 fan is an unknown quantity at this point 
Solved Threads: 11
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Re: Get local IP address

 
0
  #4
Jan 21st, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 6
Reputation: Bill the Cat is an unknown quantity at this point 
Solved Threads: 0
Bill the Cat Bill the Cat is offline Offline
Newbie Poster

Re: Get local IP address

 
0
  #5
Jan 22nd, 2006
Originally Posted by f1 fan
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 6
Reputation: Bill the Cat is an unknown quantity at this point 
Solved Threads: 0
Bill the Cat Bill the Cat is offline Offline
Newbie Poster

Re: Get local IP address

 
0
  #6
Jan 23rd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 6
Reputation: Bill the Cat is an unknown quantity at this point 
Solved Threads: 0
Bill the Cat Bill the Cat is offline Offline
Newbie Poster

Re: Get local IP address

 
0
  #7
Jan 31st, 2006
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC