Solved

Disable and Re-Enable LAN Adapter with VB.NET

Posted on 2011-02-22
2
2,331 Views
Last Modified: 2012-05-11
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
Comment
Question by:BMcCubbin
2 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 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
  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
 
LVL 3

Author Comment

by:BMcCubbin
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.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows explorer default details view 10 82
vb.net and creating a class 5 20
vb.net cycle through results of a sql query and capture variables 3 18
MailAddress in vb 4 29
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 …
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
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…

838 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