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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JoachimPetersenAuthor Commented:
helped me.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.