locked
Win32_NetworkAdapterConfiguration , NIC Selected Index problem RRS feed

  • Question

  • Hi.

    I have designed I.P Change utility in VS2012.

    My problem is " I can only change the I.P of very first NIC "

    I tried >> Win32_NetworkAdapterConfiguration.index=0 , but getting error

    my function is as under ..

        Private Sub Change_Click(sender As Object, e As EventArgs) Handles Change.Click

            '  ComboBox1.SelectedIndex
            Dim Sno As Integer = 0
            Dim IPAddress As String = (ChangeIp.Text)
            Dim SubnetMask As String = (ChangeSn.Text)
            Dim Gateway As String = (ChangeGw.Text)

            Dim adapterConfig = New ManagementClass("Win32_NetworkAdapterConfiguration")
            Dim networkCollection = adapterConfig.GetInstances()

            '      For each (ManagementObject adapter in networkCollection)
            For Each adaptor As ManagementObject In networkCollection
                DeviceName01.Text = adaptor.ToString

                If (Not CBool(adaptor("IPEnabled"))) Then : Continue For : End If   'Jump to next adaptor

                Try
                    Dim NewIP As ManagementBaseObject = Nothing
                    Dim NewGate As ManagementBaseObject = Nothing

                    'Set DefaultGateway
                    NewGate = adaptor.GetMethodParameters("SetGateways")
                    NewGate("DefaultIPGateway") = New String() {Gateway}
                    NewGate("GatewayCostMetric") = New Integer() {1}

                    'Set IPAddress and Subnet Mask
                    NewIP = adaptor.GetMethodParameters("EnableStatic")
                    NewIP("IPAddress") = New String() {IPAddress}
                    NewIP("SubnetMask") = New String() {SubnetMask}

                    adaptor.InvokeMethod("EnableStatic", NewIP, Nothing)
                    adaptor.InvokeMethod("SetGateways", NewGate, Nothing)

                Catch ex As Exception
                    MessageBox.Show("Unable to Set IP : " & ex.Message)
                End Try
            Next adaptor

            Beep()
            MessageBox.Show("Updated IPAddress, SubnetMask and Default Gateway !")

        End Sub

    ****************

    Much thankful if any body can help me !!

    regards

    imran

    Monday, August 8, 2016 6:52 PM

Answers

  • Hi Imran N.S,

    We could use ObjectQuery method to retrieve the collections, like this:

     '  ComboBox1.SelectedIndex
            Dim Sno As Integer = 0
            Dim IPAddress As String = (ChangeIp.Text)
            Dim SubnetMask As String = (ChangeSn.Text)
            Dim Gateway As String = (ChangeGw.Text)
    
            'Dim adapterConfig = New ManagementClass("Win32_NetworkAdapterConfiguration")
            'Dim networkCollection = adapterConfig.GetInstances()
    
            Dim query As ObjectQuery = New ObjectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration")
    
            Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher(query)
    
            Dim networkCollection As ManagementObjectCollection = searcher.Get
    
    
            '      For each (ManagementObject adapter in networkCollection)
            For Each adaptor As ManagementObject In networkCollection
                DeviceName01.Text = adaptor.ToString
    
                If (Not CBool(adaptor("IPEnabled"))) Then : Continue For : End If   'Jump to next adaptor
    
                Try
                    Dim NewIP As ManagementBaseObject = Nothing
                    Dim NewGate As ManagementBaseObject = Nothing
    
                    'Set DefaultGateway
                    NewGate = adaptor.GetMethodParameters("SetGateways")
                    NewGate("DefaultIPGateway") = New String() {Gateway}
                    NewGate("GatewayCostMetric") = New Integer() {1}
    
                    'Set IPAddress and Subnet Mask
                    NewIP = adaptor.GetMethodParameters("EnableStatic")
                    NewIP("IPAddress") = New String() {IPAddress}
                    NewIP("SubnetMask") = New String() {SubnetMask}
    
                    adaptor.InvokeMethod("EnableStatic", NewIP, Nothing)
                    adaptor.InvokeMethod("SetGateways", NewGate, Nothing)
    
                Catch ex As Exception
                    MessageBox.Show("Unable to Set IP : " & ex.Message)
                End Try
            Next adaptor
    
            Beep()
            MessageBox.Show("Updated IPAddress, SubnetMask and Default Gateway !")

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 9, 2016 3:26 AM