• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2697
  • Last Modified:

Disable and Re-Enable LAN Adapter with VB.NET

Hello,

I have an application I am working on where I need to Disable and Re-Enable the Local Area Connection using VB.Net.  I am using Visual Studio 2010, and have been able to release/renew the IP, but found that I need to Disable the actual adapter, then re-enable it.  Or even if there is a way to programmatically run the windows network repair.  Any help would be appreciated.  I need to do this on Windows XP machines.
0
BMcCubbin
Asked:
BMcCubbin
1 Solution
 
DhaestCommented:
Source found at experts-exchange: http://www.experts-exchange.com/Programming/System/Windows__Programming/Q_22812895.html
VB.NET translation:

' Add a COM reference to 'Microsoft Shell Controls And Automation' to the project. 
' Source: 
' http://channel9.msdn.com/ShowPost.aspx?PostID=158556 
' http://channel9.msdn.com/Photos/ZippedFiles/158556_ToggleNic.zip 

Imports System
Imports System.IO
Imports System.Threading
Imports Shell32

Public Enum AdapterError
  Success
  ConnectionNotFound
  ConnectionsFolderNotFound
  ConnectionAlreadyOpen
  ConnectionAlreadyClosed
End Enum

Public Class NetworkAdapterState

  Public Shared Function SetConnectionState(ByVal connectionFolder As String, ByVal connectionName As String, ByVal enableVerb As String, ByVal disableVerb As String, ByVal enable As Boolean) As AdapterError
    Dim [error] As AdapterError = AdapterError.Success

    Dim networkConnectionsFolder As Folder = GetNetworkConnectionsFolder(connectionFolder)
    If networkConnectionsFolder Is Nothing Then
      [error] = AdapterError.ConnectionsFolderNotFound
    Else
      Dim networkConnection As FolderItem2 = GetNetworkConnection(networkConnectionsFolder, connectionName)

      If networkConnection Is Nothing Then
        [error] = AdapterError.ConnectionNotFound
      Else
        Dim verb As FolderItemVerb = Nothing
        If IsNetworkConnectionEnabled(networkConnection, enableVerb, disableVerb, verb) Then
          If enable Then
            [error] = AdapterError.ConnectionAlreadyOpen
          Else
            verb.DoIt()
            Thread.Sleep(2000)
          End If
        Else
          If Not enable Then
            [error] = AdapterError.ConnectionAlreadyClosed
          Else
            verb.DoIt()
            Thread.Sleep(2000)
          End If
        End If
      End If
    End If

    Return [error]
  End Function

  ''' <summary> 
  ''' Gets the Network Connections folder in the control panel. 
  ''' </summary> 
  Private Shared Function GetNetworkConnectionsFolder(ByVal folderName As String) As Folder
    Dim sh As New Shell()

    ' Get a reference to the Control panel. 
    Dim controlPanel As Folder = sh.[NameSpace](3)

    Dim items As FolderItems = controlPanel.Items()
    For Each item As FolderItem In items
      If item.Name = folderName Then
        Return TryCast(item.GetFolder, Folder)
      End If
    Next

    Return Nothing
  End Function

  ''' <summary> 
  ''' Gets the network connection with the specified name from the specified shell folder. 
  ''' </summary> 
  Private Shared Function GetNetworkConnection(ByVal networkConnectionsFolder As Folder, ByVal connectionName As String) As FolderItem2
    Dim items As FolderItems = networkConnectionsFolder.Items()
    For Each item As FolderItem2 In items
      If item.Name = connectionName Then
        Return item
      End If
    Next

    Return Nothing
  End Function

  ''' <summary> 
  ''' Gets whether or not the network connection is enabled and the command to enable/disable it. 
  ''' </summary> 
  Private Shared Function IsNetworkConnectionEnabled(ByVal networkConnection As FolderItem2, ByVal enableVerb As String, ByVal disableVerb As String, ByRef enableDisableVerb As FolderItemVerb) As Boolean
    enableDisableVerb = Nothing

    Dim verbs As FolderItemVerbs = networkConnection.Verbs()
    For Each verb As FolderItemVerb In verbs
      If verb.Name = enableVerb Then
        enableDisableVerb = verb
        Return False
      ElseIf verb.Name = disableVerb Then
        enableDisableVerb = verb
        Return True
      End If
    Next
    Return False
  End Function

End Class

Open in new window

0
 
BMcCubbinAuthor Commented:
Thanks for the response.  Could you give me some ideas of how the syntax would be of using these functions.  I have a private sub that I want if a radiobutton is clicked to disable and then re-enable the NIC.   I am far from an expert at .NET development, and just need an example of how you would execute this.  Thanks for your help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now