[Webinar] Streamline your web hosting managementRegister Today

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

Getting error when trying to disable/enable a network adapter


I am getting error when I try to disable/enable a network adapter.

Getting Error: NullReferenceException was unhandled.

Thanks in advance

add reference "shell32.dll"
Imports Shell32

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sc As New Shell32.ShellClass()
        Dim RootFolder As Shell32.Folder = sc.[NameSpace](Shell32.ShellSpecialFolderConstants.ssfCONTROLS)
        Dim SrcFlder As Shell32.Folder = Nothing
        Dim Adapter As String = "Local Area Network"
        Dim fItem As ShellFolderItem = Nothing

        For Each fi As Shell32.FolderItem2 In RootFolder.Items()
            If fi.Name = "Network Connections" Then
                SrcFlder = DirectCast(fi.GetFolder, Shell32.Folder)
                Exit For
            End If

        If SrcFlder Is Nothing Then
            Console.WriteLine("SrcFlder ""Network Connections""")
        End If

        For Each fi As Shell32.FolderItem In SrcFlder.Items()
            If fi.Name = Adapter Then
                fItem = DirectCast(fi, ShellFolderItem)
                Exit For
            End If

        If fItem Is Nothing Then
            Console.WriteLine("Adapter """ & Adapter & """ doesn't")
        End If

        For Each fi As Shell32.FolderItemVerb In fItem.Verbs()
            Dim tempStat As String = String.Empty
            '0 - to disable adapter
            '1 - to enable adapter
            Dim newState As Integer = 1
            Select Case newState
                Case 0
                    tempStat = "disa&ble"
                    newState = 22
                    Exit Select
                Case 1
                    tempStat = "en&able"
                    newState = 0
                    Exit Select
            End Select

            If String.Compare(fi.Name, tempStat, True) = 0 Then
                'set adapter's state
                Console.WriteLine("Adapter was " & tempStat.Replace("&", "") & "d")
                Exit Sub
            End If
            Console.WriteLine("Adapter wasn't found")
    End Sub
End Class

Open in new window

  • 3
  • 3
1 Solution
fi.DoIt() Include a check for null (nothing) before calling this method. If it does not resolve then please explain in more detail which line is throwing error.
JoachimPetersenAuthor Commented:
here is a image of the error and the line where the error start.
Looks like you are using WIN XP and in windows xp the adapter name is "Local Area Connection" and not "Local Area Network" Change the adapter string and try
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

JoachimPetersenAuthor Commented:
Yes the image was from a xp but I only made screenshot of it on a xp because I had my OS on english on my xp. still by renaming the string it on the xp it seem to work fine but I cant get it to work on vista. anyways if no other solutions will be posted I will accept the above statement as a solution :P

if there is anyother way to restart a adapter I would also take that as a solution as my goal is to make a code to restart a adapter.
Yes. It does not work on Vista. Infact I tried to test it in vista first and could not get to the point u mentioned and looked at your snapshot and tested in XP.

In Vista, the network connections are organized a bit different though. So you cannot have the same code working for both.

You would have to take a different code path for vista and above.

Let me see if i can get you some link of an example
JoachimPetersenAuthor Commented:
helped me.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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