Author Topic: Network adapters - change local IP address  (Read 636 times)

Phrog30

  • Guest
Network adapters - change local IP address
« on: May 11, 2017, 09:17:21 AM »
I'm able to get a list of all adapters and their related info:
Code: [Select]
Private Sub cmb_Load()

        cmb_NICs.Items.Clear()

        For Each nic As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
            If (nic.NetworkInterfaceType = NetworkInterfaceType.Ethernet) OrElse (nic.NetworkInterfaceType = NetworkInterfaceType.Wireless80211) Then
                cmb_NICs.Items.Add(nic.Name)
            End If
        Next

    End Sub

    Private Sub cmb_NICs_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmb_NICs.SelectedIndexChanged

        LanSetting_Load()

    End Sub

    Private Sub LanSetting_Load()

        For Each nic As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
            If nic.Name = cmb_NICs.Text Then

                If (nic.NetworkInterfaceType = NetworkInterfaceType.Ethernet) OrElse (nic.NetworkInterfaceType = NetworkInterfaceType.Wireless80211) Then
                    txt_Name.Text = nic.Name
                    txt_Description.Text = nic.Description

                    For Each ip As UnicastIPAddressInformation In nic.GetIPProperties().UnicastAddresses
                        If nic.Description = txt_Description.Text Then
                            If ip.Address.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
                                txt_IPAddress.Text = ip.Address.ToString
                                txt_Mask.Text = ip.IPv4Mask.ToString
                            End If
                        End If
                    Next

                    txt_Status.Text = nic.OperationalStatus.ToString
                    txt_Type.Text = nic.NetworkInterfaceType.ToString

                End If

            End If
        Next

    End Sub

What I'm not able to do is change the mask, IP, etc. to these adapters.  I tried this code but it didn't work.  I didn't see any errors, but nothing happened either:

Code: [Select]
Public Sub setIP(ip_address As String, subnet_mask As String)
        Dim objMC As New ManagementClass("Win32_NetworkAdapterConfiguration")
        Dim objMOC As ManagementObjectCollection = objMC.GetInstances()

        For Each objMO As ManagementObject In objMOC
            If CBool(objMO("IPEnabled")) Then
                Try
                    Dim setIP As ManagementBaseObject
                    Dim newIP As ManagementBaseObject = objMO.GetMethodParameters("EnableStatic")

                    newIP("IPAddress") = New String() {ip_address}
                    newIP("SubnetMask") = New String() {subnet_mask}

                    setIP = objMO.InvokeMethod("EnableStatic", newIP, Nothing)
                Catch ex As Exception

                    MessageBox.Show(ex.Message + System.Environment.NewLine, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Throw

                End Try
            End If
        Next

Has anyone successfully changed the IP, mask on the local box in .net?  If so, would you be willing to share your code?

James