|
-
May 21st, 2007, 08:53 PM
#1
Thread Starter
Addicted Member
[RESOLVED] [2005] WMI IPAddress Array Help
Hi Everyone,
I need your help with breaking out data within an array. The below code is a WMI query on your local machine that should output your IP addresses via a message box.
My problem is that when I run the code, the message box displays "System.String[]". I know this is because machines may have multiple IPs and they stored in this array.
My question is, how do I output the data within this array? ANY help would be greatly appreciated.
Here is a link from Microsoft outlining the IPAddress aray http://msdn2.microsoft.com/en-us/library/aa394217.aspx
Code:
Imports System.Management
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim manScope As ManagementScope
Dim manPath As ManagementPath
Dim manClass As ManagementClass
Dim manObj As ManagementObject
Dim manObjCol As ManagementObjectCollection
Dim output As String = String.Empty
Dim RemoteMachineName As String
RemoteMachineName = "."
Try
manPath = New ManagementPath("\\" & RemoteMachineName & "\root\CIMV2:Win32_NetworkAdapterConfiguration")
manScope = New ManagementScope(manPath)
manClass = New ManagementClass(manScope, manPath, Nothing)
manObjCol = manClass.GetInstances()
For Each manObj In manObjCol
MsgBox(manObj("IPAddress").ToString)
Next
Catch ex As Exception
' MessageBox.Show(ex.Message)
Finally
manPath = Nothing
manScope = Nothing
manClass = Nothing
manObjCol = Nothing
End Try
End Sub
End Class
Last edited by Giraffe Frenzy; May 21st, 2007 at 08:58 PM.
-
May 21st, 2007, 08:58 PM
#2
Re: [2005] WMI IPAddress Array Help
You would normally use a For Each loop or a For loop to visit each element of an array. This is no different.
-
May 21st, 2007, 08:59 PM
#3
Thread Starter
Addicted Member
Re: [2005] WMI IPAddress Array Help
Could you show me how that would be done in this case? I am having issues getting it to work.
-
May 21st, 2007, 09:52 PM
#4
Re: [2005] WMI IPAddress Array Help
If you've ever done it for one String array then you already know how to do it. What code are you using?
-
May 22nd, 2007, 04:59 AM
#5
Thread Starter
Addicted Member
Re: [2005] WMI IPAddress Array Help
Hi jmc,
That is my problem. I do not know how to do this. I understand the concept, but I do not know how to relate it to this situation.
-
May 22nd, 2007, 05:07 AM
#6
Thread Starter
Addicted Member
Re: [2005] WMI IPAddress Array Help
jmc,
I tried outputting the data in the array by using another for loop, but this did not work:
Code:
Imports System.Management
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim manScope As ManagementScope
Dim manPath As ManagementPath
Dim manClass As ManagementClass
Dim manObj As ManagementObject
Dim manObjCol As ManagementObjectCollection
Dim output As String = String.Empty
Dim RemoteMachineName As String
Dim ip As String
RemoteMachineName = "."
Try
manPath = New ManagementPath("\\" & RemoteMachineName & "\root\CIMV2:Win32_NetworkAdapterConfiguration")
manScope = New ManagementScope(manPath)
manClass = New ManagementClass(manScope, manPath, Nothing)
manObjCol = manClass.GetInstances()
For Each manObj In manObjCol
For Each ip In (manObj("IPAddress").ToString)
MsgBox(manObj("IPAddress").ToString)
Next
Next
Catch ex As Exception
' MessageBox.Show(ex.Message)
Finally
manPath = Nothing
manScope = Nothing
manClass = Nothing
manObjCol = Nothing
End Try
End Sub
End Class
-
May 22nd, 2007, 06:34 AM
#7
Re: [2005] WMI IPAddress Array Help
We've already established that manObj("IPAddress") is a String array and that manObj("IPAddress").ToString returns "System.String[]". If you want to get the elements from an array you don't want to turn that array into a string first. You want to use the array itself. Read these words very carefully. You want to do something:
for each string in the array
Now, with those words in mind, look at your code again and ask yourself how you would change your For Each loop to implement those words.
-
May 22nd, 2007, 06:38 AM
#8
Thread Starter
Addicted Member
Re: [2005] WMI IPAddress Array Help
Thanks JMC for your help. Your style and persistence in teaching is very very helpful. I got it working with the below for loop.
Code:
For Each ip In manObj("IPAddress")
MsgBox(ip)
Next
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|