Dim LANEnable As Boolean = True
' ***** Either placed within a button or routine *****
' Control Panel Identifier
Const ssfCONTROLS = 3
' Enter the name of the connection to manipulate
Dim ConnectionName As String = "Local Area Connection"
Dim EnableVerb As String = "En&able"
Dim DisableVerb As String = "Disa&ble"
' Generate Shell item
Dim ShellApp As New Shell32.Shell()
' Obtain the CP
Dim ControlPanel As Shell32.Folder = ShellApp.NameSpace(ssfCONTROLS)
Dim FolderItem As Shell32.FolderItem
Dim NetworkFolder As Shell32.Folder
Dim LANConnection As Shell32.FolderItem
' Loop through the items in the control panel and obtain the Network Connections folder
For Each FolderItem In ControlPanel.Items()
Debug.WriteLine("Loop 1: " & FolderItem.Name)
If FolderItem.Name = "Network Connections" Then
' When found - exit the loop
NetworkFolder = FolderItem.GetFolder
Exit For
End If
Next
' Debug check
If NetworkFolder Is Nothing Then
MessageBox.Show("Error - No network folder found")
Exit Sub
End If
' Obtain the appropriate connection record
For Each FolderItem In NetworkFolder.Items()
Debug.WriteLine("Loop 2: " & FolderItem.Name)
If FolderItem.Name = ConnectionName Then
' When found - exit the loop
LANConnection = FolderItem
Exit For
End If
Next
' Debug check
If LANConnection Is Nothing Then
MessageBox.Show("Error - No LAN entry was not found")
Exit Sub
End If
' Swtich the LAN toggle
LANEnable = Not LANEnable
Dim EnableVerbItem, DisableVerbItem, Verb As Shell32.FolderItemVerb
' Run through all available options and obtain the appropriate action
For Each Verb In LANConnection.Verbs
Debug.WriteLine("Loop 3: " & Verb.Name)
If Verb.Name = EnableVerb Then
EnableVerbItem = Verb
End If
If Verb.Name = DisableVerb Then
DisableVerbItem = Verb
End If
Next
' Perform the enable / disable
If LANEnable Then
EnableVerbItem.DoIt()
Else
DisableVerbItem.DoIt()
End If