Hello, I am attempting to write a program that will disable the Local Area Connection while leaving the Wireless connection enabled. My company is using net-books in our stores to demo air-cards and we need to keep them off of the LAN. I'm new to VB and VB.net, and it's been a while since I did any type of programming. I've gathered some code that others have posted in forums and tutorials and I'm working through a tutorial book, but I finally believe that I am stuck.

I have a form that contains a user name and password field and a log on button as well as an exit button. Once the user name and password are input and the log on button in pushed, it should call up the 'ToggleNetworkConnection.vb' class and execute the code that is in there. Any help would be much appreciated! Once again, I'm fairly new to this, so if you see any ways to make this more efficient, please feel free to offer criticism!

Here is the code for the Form1

Public Class Form1
    Inherits System.Windows.Forms.Form
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Username As String
        Dim Password As String
        Dim DisablePort As ToggleNetworkConnection()

        Username = "admin" 'username box
        Password = "admin" 'password box

        If TextBox1.Text = Username And TextBox2.Text = Password Then 'make sure they authenticate
            'this is where I believe the code to call the TogglenetworkConnection.vb should go
        End If
    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        TextBox2.PasswordChar = "*" 'makes the password show up as * instead of letters
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'insert code to exit the program
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class

Here is the code for the ToggleNetworkConnection.vb class

Public Class ToggleNetworkConnection

#Region "Public Methods"

    Public Shared Sub ToggleLocalAreaConnection()
        For Each verb As Shell32.FolderItemVerb In LocalAreaConnectionFolderItem.Verbs
            If verb.Name = "Disa&ble" Then
                verb.DoIt()
                Exit For
            End If
        Next
        Threading.Thread.Sleep(1000)
    End Sub

#End Region

#Region "Properties"

    Private Shared ReadOnly Property ControlPanelFolder()
        Get
            Dim shell As New Shell32.Shell()
            Return shell.NameSpace(3)
        End Get
    End Property

    Private Shared ReadOnly Property NetworkFolder() As Shell32.Folder
        Get

            Dim retVal As Shell32.Folder = Nothing

            For Each fi As Shell32.FolderItem In ControlPanelFolder.Items
                If fi.Name = "Network Connections" Then
                    retVal = fi.GetFolder()
                End If
            Next

            If retVal Is Nothing Then
                Throw New NetworkConnectionsFolderNotFoundException()
            Else
                Return retVal
            End If

        End Get
    End Property

    Private Shared ReadOnly Property LocalAreaConnectionFolderItem() As Shell32.FolderItem
        Get

            Dim retVal As Shell32.FolderItem = Nothing

            For Each folderItem As Shell32.FolderItem In NetworkFolder.Items
                Console.WriteLine(folderItem.Name)
                If InStr(folderItem.Name.ToLower(), "Local Area Connection".ToLower()) Then
                    retVal = folderItem
                    Exit For
                End If
            Next

            If retVal Is Nothing Then
                Throw New LocalAreaConnectionFolderItemNotFoundException()
            Else
                Return retVal
            End If

        End Get
    End Property

#End Region

#Region "Custom Exceptions"

    Public Class NetworkConnectionsFolderNotFoundException
        Inherits Exception

        Public Overrides ReadOnly Property Message() As String
            Get
                Return "The Network Connections Folder Could Not Be Found!"
            End Get
        End Property
    End Class

    Public Class LocalAreaConnectionFolderItemNotFoundException
        Inherits Exception

        Public Overrides ReadOnly Property Message() As String
            Get
                Return "The Local Area Connection Folder Could Not Be Found!"
            End Get
        End Property
    End Class

#End Region

End Class

Here is the code for the Module1 which I believe calls Form1

Module Module1
    Dim MainMenu As New Form1()

    Sub Main()
        MainMenu.Show()

    End Sub

End Module

Once again, any help would be great!

Thanks,

JackHadding

If you are wanting to disallow access to LAN, but keep access to Wireless, consider disabling the LAN cards themselves through Device Manager in Windows.
In this case, the Local Area Connection item won't appear in the Network Adapters list, and when a cable is inserted in to LAN, no connection will be made...

Also, the benefits of this is that Users that are not Power Users or Administrators will not be able to re-enable the device as Device Manager requires Administrative Privileges.

You're right! I spent some more time reading his instructions and figured out where I went wrong. I guess that I forgot to reference the Shell32.dll! Now I'm able to run this on Win XP, but it won't run on Win 7 because the folder path is different. I'm going to hopefully work on this over the next few days and I'll post what I have so that anyone else who want to use this can. Also, I made some changes to the overall program, like instead of calling a different class, it will just run inside of the current form. (I think that makes sense)... After I finish, I'll post the code and then a link to where the .zip can be downloaded from.

I'm always open to suggestions on how something can be made more efficient, so please feel free to offer advice/criticism.

Thanks,
JackHadding

Okay, so I reworked some of the code and made multiple changes to the last bit I posted. This is a working version for Windows XP. I did take a lot of the code from http://www.codeproject.com/KB/vb/toggleNetworkConn.aspx so, thanks to him for posting it! I now am running into the problem of Windows 7 not agreeing with the path for the Network Folder. I've done some looking and can't seem to find it. I tried using this path (Control Panel\All Control Panel Items\Network and Sharing Center) as well as (Control Panel\Network and Internet\network Connections) but that doesn't work either. Any help would be great! Below is the code. Form1 will send you to form2 which lets you enable or disable the network port.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Username As String
        Dim Password As String

        Username = "admin"
        Password = "admin"

        If TextBox1.Text = Username And TextBox2.Text = Password Then
            Form2.Show()
            Me.Hide()
            MsgBox("Thanks for logging in ")
        End If
    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        TextBox2.PasswordChar = "*"
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Application.Exit()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class
Public Class Form2


    Inherits System.Windows.Forms.Form
    Private Shared ReadOnly Property ControlPanelFolder() As Shell32.Folder
        Get
            Dim shell As New Shell32.Shell()
            Return shell.NameSpace(3)
        End Get
    End Property

    Private Shared ReadOnly Property NetworkFolder() As Shell32.Folder
        Get
            Dim retVal As Shell32.Folder = Nothing

            For Each Fi As Shell32.FolderItem In ControlPanelFolder.Items
                If Fi.Name = "Network Connections" Then
                    retVal = Fi.GetFolder()
                End If
            Next
            If retVal Is Nothing Then
                Throw New NetworkConnectionsFolderNotFoundException()
            Else
                Return retVal
            End If
        End Get
    End Property

    Private Shared ReadOnly Property LocalAreaConnectionFolderItem() As Shell32.FolderItem
        Get
            Dim retVal As Shell32.FolderItem = Nothing

            For Each folderItem As Shell32.FolderItem In NetworkFolder.Items
                Console.WriteLine(folderItem.Name)
                If InStr(folderItem.Name.ToLower(), "Local Area Connection".ToLower()) Then
                    retVal = folderItem
                    Exit For
                End If
            Next
            If retVal Is Nothing Then
                Throw New LocalAreaConnectionFolderItemNotFoundException()
            Else
                Return retVal
            End If
        End Get
    End Property

    Public Shared Sub DisableLocalAreaConnection()
        For Each Verb As Shell32.FolderItemVerb In LocalAreaConnectionFolderItem.Verbs
            If Verb.Name = "Disa&ble" Then
                Verb.DoIt()
                Exit For
            End If
        Next
        Threading.Thread.Sleep(1000)
    End Sub

    Public Shared Sub EnableLocalAreaConnection()
        For Each Verb As Shell32.FolderItemVerb In LocalAreaConnectionFolderItem.Verbs
            If Verb.Name = "En&able" Then
                Verb.DoIt()
                Exit For
            End If
        Next
        Threading.Thread.Sleep(1000)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DisableLocalAreaConnection()
        TextBox1.Text = "Ethernet Port 1 Has Been Disabled"

    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Application.Exit()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        EnableLocalAreaConnection()
        TextBox1.Text = "Ethernet Port 1 Has Been Enabled"
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class

#Region "Custom Exceptions"
Public Class NetworkConnectionsFolderNotFoundException
    Inherits Exception

    Public Overrides ReadOnly Property Message() As String
        Get
            Return "The Network Connections Folder Could Not Be Found!"
        End Get
    End Property

End Class

Public Class LocalAreaConnectionFolderItemNotFoundException
    Inherits Exception
    Public Overrides ReadOnly Property Message() As String
        Get
            Return "The Local Area Connection Folder Could Not Be Found!"
        End Get
    End Property
End Class
#End Region
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.