Solved

Disable and Re-Enable LAN Adapter with VB.NET

Posted on 2011-02-22
2
2,212 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now