Disable and Re-Enable LAN Adapter with VB.NET

Posted on 2011-02-22
Medium Priority
Last Modified: 2012-05-11

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.
Question by:BMcCubbin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 53

Accepted Solution

Dhaest earned 2000 total points
ID: 34951165
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
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
      Dim networkConnection As FolderItem2 = GetNetworkConnection(networkConnectionsFolder, connectionName)

      If networkConnection Is Nothing Then
        [error] = AdapterError.ConnectionNotFound
        Dim verb As FolderItemVerb = Nothing
        If IsNetworkConnectionEnabled(networkConnection, enableVerb, disableVerb, verb) Then
          If enable Then
            [error] = AdapterError.ConnectionAlreadyOpen
          End If
          If Not enable Then
            [error] = AdapterError.ConnectionAlreadyClosed
          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

    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

    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
    Return False
  End Function

End Class

Open in new window


Author Comment

ID: 34954898
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.

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If your system is showing symptoms of browser hijacks or 'google search redirects' check out my other article (http://rdsrc.us/u3GP7A) first and run the tool TDSSKiller (http://rdsrc.us/GDBBs4) to get rid of the infection. Once done, and if the …
It is only natural that we all want our PCs to be in good working order, improved system performance, so that is exactly how programs are advertised to entice. They say things like:            •      PC crashes? Get registry cleaner to repair it!    …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question