Getting error when trying to disable/enable a network adapter

Hey.

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"
(windows/system32/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
        Next

        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
        Next

        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
                fi.DoIt()
                Console.WriteLine("Adapter was " & tempStat.Replace("&", "") & "d")
                Exit Sub
            End If
            Console.WriteLine("Adapter wasn't found")
        Next
    End Sub
End Class

Open in new window

JoachimPetersenAsked:
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.

SriVaddadiCommented:
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.
0
JoachimPetersenAuthor Commented:
here is a image of the error and the line where the error start.
untitled.bmp
0
SriVaddadiCommented:
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
0
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.
0
SriVaddadiCommented:
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
0

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.
0
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.