Link to home
Start Free TrialLog in
Avatar of JoachimPetersen
JoachimPetersenFlag for Denmark

asked on

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

Avatar of Ravi Vaddadi
Ravi Vaddadi
Flag of United States of America image

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.
Avatar of JoachimPetersen

ASKER

here is a image of the error and the line where the error start.
untitled.bmp
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
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.
ASKER CERTIFIED SOLUTION
Avatar of Ravi Vaddadi
Ravi Vaddadi
Flag of United States of America image

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
helped me.