How to get the current workstation IP# & Gateway IP#?

steve728

New member
Joined
Jul 2, 2004
Messages
2
Programming Experience
Beginner
Please show me an example of how to get the current work station's IP address and the Gateway IP address.

Thanks in advance.

Steve
 

vbkev

Active member
Joined
Jun 4, 2004
Messages
32
Location
Newcastle, UK
Programming Experience
5-10
Add a reference to System.Management then use this function

Private Sub GetIPDetails()

Dim oSearcher As New System.Management.ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration")

Dim oObject As System.Management.ManagementObject

Dim sIP() As String

Dim sGateWay() As String

Dim sString As String

Dim sMessage As String

For Each oObject In oSearcher.Get

If Not oObject("ipaddress") Is Nothing Then sIP = oObject("ipaddress")

If Not oObject("defaultipgateway") Is Nothing Then sGateWay = oObject("defaultipgateway")

Next

For Each sString In sIP

sMessage += "IP Address: " + sString + vbCrLf

Next

For Each sString In sGateWay

sMessage += "Default Gateway Address: " + sString + vbCrLf

Next

MsgBox(sMessage)

End Sub

 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
How can I loop through the object properties, I don't know where to begin. Do you have a list of the properties I could look at, thanks.

Jon
 

vbkev

Active member
Joined
Jun 4, 2004
Messages
32
Location
Newcastle, UK
Programming Experience
5-10
List Properties

VB.NET:
[size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] ListObjects()
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] oSearcher [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Management.ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration")
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] oObject [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Management.ManagementObject
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] oProperty [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Management.PropertyData
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] sKey [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = ""
 
[/size][size=2][color=#0000ff]For [/color][/size][size=2][color=#0000ff]Each[/color][/size][size=2] oObject [/size][size=2][color=#0000ff]In[/color][/size][size=2] oSearcher.Get
	[/size][size=2][color=#0000ff]For [/color][/size][size=2][color=#0000ff]Each[/color][/size][size=2] oProperty [/size][size=2][color=#0000ff]In[/color][/size][size=2] oObject.Properties
		sKey += oProperty.Name + " : "
		[/size][size=2][color=#0000ff]If[/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2] oProperty.Value [/size][size=2][color=#0000ff]Is [/color][/size][size=2][color=#0000ff]Nothing [/color][/size][size=2][color=#0000ff]Then
			[/color][/size][size=2]sKey += oProperty.Value + vbCrLf
		[/size][size=2][color=#0000ff]Else
			[/color][/size][size=2]sKey += "Nothing" + vbCrLf
		[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
	[/color][/size][size=2][color=#0000ff]Next
[/color][/size][size=2][color=#0000ff]Next
 
[/color][/size][size=2]MsgBox(sKey)
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size]
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
VB.NET:
[size=2][color=#0000ff]If [/color][/size][size=2][color=#0000ff]Not[/color][/size][size=2] oProperty.Value [/size][size=2][color=#0000ff]Is [/color][/size][size=2][color=#0000ff]Nothing [/color][/size][size=2][color=#0000ff]Then
			[/color][/size][size=2]sKey += oProperty.Value + vbCrLf <----[b]On this line I get an error[/b]
	[/size][size=2][color=#0000ff]Else
			[/color][/size][size=2]sKey += "Nothing" + vbCrLf
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]If[/color][/size]

The error is:
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from string " " to type 'Double' is not valid.
 
Top Bottom