Link to home
Start Free TrialLog in
Avatar of XK8ER
XK8ERFlag for United States of America

asked on

vb.net - disable nic

hello there,
how can I disable the local area connection using vb.net
Avatar of ong-hh
ong-hh

Avatar of Deepak Lakkad
Hi

Shell command will allow you to execute and command using VB Application.

For Example, if you want to open NotePad using your VB application then Shell Command will be written as below:

Shell("C:\WINDOWS\system32\notepad.exe")

Following Link will help you to enable/disable network connection using Command Line.

https://www.experts-exchange.com/questions/22455419/How-to-disable-a-network-connection-by-command-line-in-winxp.html

Hope this will help

- Deepak Lakkad

Avatar of XK8ER

ASKER

I am looking for a different approach than using Shell
Avatar of XK8ER

ASKER

on one of the links posted I see this code.. but how exactly do I use it?
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

NetworkAdapterState.SetConnectionState("Network Connections", "Wireless Network Connection", "En&able", "Disa&ble", True)

"Network Connections" = item name of the network in the Control Panel
"Wireless Network Connection" = The network name to enable or disable
"En&able", "Disa&ble" = the verb to enable and disable the network
True = enable the network
False = disable the network

ASKER CERTIFIED SOLUTION
Avatar of ong-hh
ong-hh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of XK8ER

ASKER

ong-hh, im not trying to find a local area connection im trying to disable it..

im trying the code that I posted with this

NetworkAdapterState.SetConnectionState("Network Connections", "Wireless Network Connection", "En&able", "Disa&ble", True)

but its not working using it on windows 7.. its not finding the directory maybe the code bellow needs to be upgraded?
Public 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

Open in new window