943,838 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 91151
  • VB.NET RSS
Jan 19th, 2006
0

Get local IP address

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bill the Cat is offline Offline
6 posts
since Jan 2006
Jan 21st, 2006
0

Re: Get local IP address

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.
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006
Jan 21st, 2006
0

Re: Get local IP address

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bill the Cat is offline Offline
6 posts
since Jan 2006
Jan 21st, 2006
0

Re: Get local IP address

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
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006
Jan 22nd, 2006
0

Re: Get local IP address

Quote 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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bill the Cat is offline Offline
6 posts
since Jan 2006
Jan 23rd, 2006
0

Re: Get local IP address

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bill the Cat is offline Offline
6 posts
since Jan 2006
Jan 31st, 2006
0

Re: Get local IP address

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bill the Cat is offline Offline
6 posts
since Jan 2006
Aug 10th, 2010
0
Re: Get local IP address
HttpContext.Current.Request.UserHostAddres

this will give u the address
Last edited by nilaavu; Aug 10th, 2010 at 6:00 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nilaavu is offline Offline
1 posts
since Aug 2010
Nov 13th, 2011
0
Re: Get local IP address
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.......
Reputation Points: 10
Solved Threads: 0
Newbie Poster
farhanshaikh is offline Offline
1 posts
since Nov 2011

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: How Can I Send Email With Embedded Images
Next Thread in VB.NET Forum Timeline: Combox to populate Textbox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC