Results 1 to 8 of 8

Thread: [RESOLVED] [2005] WMI IPAddress Array Help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    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
  •  



Click Here to Expand Forum to Full Width