Set ethernet adapter to DHCP in Visual Basic application


I need to be able to set an ethernet adapter to DHCP in a visual basic application,

Is anyone able to assist?

Who is Participating?
This can be easily done with Windows Management Instrumentation (WMI)...  The toughest part is figuring out which network adpater (there will be several psuedo adapters) to use.   In the example below, it turns on DHCP for all adapters that support IP. 

Imports System
Imports System.Management
Imports System.Windows.Forms
Namespace WMISample
    Public Class CallWMIMethod
        Public Overloads Shared Function Main() As Integer
                Dim classInstance As New ManagementObject( _
                    "root\CIMV2", _
                    "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") 
                ' Execute the method and obtain the return values.
                Dim outParams As ManagementBaseObject = _
                    classInstance.InvokeMethod("EnableDHCP", Nothing, Nothing)
                ' List outParams
                Console.WriteLine("Out parameters:")
                Console.WriteLine("ReturnValue: {0}", outParams("ReturnValue"))
            Catch err As ManagementException
                MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message)
            End Try
        End Function
    End Class
End Namespace

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.