Wednesday, April 28, 2010

Working with the Local Area Connections

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

  1. Public Class Form1
  2. Inherits System.Windows.Forms.Form
  3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4. Dim Username As String
  5. Dim Password As String
  6. Dim DisablePort As ToggleNetworkConnection()
  7. Username = "admin" 'username box
  8. Password = "admin" 'password box
  9. If TextBox1.Text = Username And TextBox2.Text = Password Then 'make sure they authenticate
  10. 'this is where I believe the code to call the TogglenetworkConnection.vb should go
  11. End If
  12. End Sub
  13. Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
  14. TextBox2.PasswordChar = "*" 'makes the password show up as * instead of letters
  15. End Sub
  16. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  17. 'insert code to exit the program
  18. End Sub
  19. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  20. End Sub
  21. Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  22. End Sub
  23. End Class

Here is the code for the ToggleNetworkConnection.vb class

  1. Public Class ToggleNetworkConnection
  2. #Region "Public Methods"
  3. Public Shared Sub ToggleLocalAreaConnection()
  4. For Each verb As Shell32.FolderItemVerb In LocalAreaConnectionFolderItem.Verbs
  5. If verb.Name = "Disa&ble" Then
  6. verb.DoIt()
  7. Exit For
  8. End If
  9. Next
  10. Threading.Thread.Sleep(1000)
  11. End Sub
  12. #End Region
  13. #Region "Properties"
  14. Private Shared ReadOnly Property ControlPanelFolder()
  15. Get
  16. Dim shell As New Shell32.Shell()
  17. Return shell.NameSpace(3)
  18. End Get
  19. End Property
  20. Private Shared ReadOnly Property NetworkFolder() As Shell32.Folder
  21. Get
  22. Dim retVal As Shell32.Folder = Nothing
  23. For Each fi As Shell32.FolderItem In ControlPanelFolder.Items
  24. If fi.Name = "Network Connections" Then
  25. retVal = fi.GetFolder()
  26. End If
  27. Next
  28. If retVal Is Nothing Then
  29. Throw New NetworkConnectionsFolderNotFoundException()
  30. Else
  31. Return retVal
  32. End If
  33. End Get
  34. End Property
  35. Private Shared ReadOnly Property LocalAreaConnectionFolderItem() As Shell32.FolderItem
  36. Get
  37. Dim retVal As Shell32.FolderItem = Nothing
  38. For Each folderItem As Shell32.FolderItem In NetworkFolder.Items
  39. Console.WriteLine(folderItem.Name)
  40. If InStr(folderItem.Name.ToLower(), "Local Area Connection".ToLower()) Then
  41. retVal = folderItem
  42. Exit For
  43. End If
  44. Next
  45. If retVal Is Nothing Then
  46. Throw New LocalAreaConnectionFolderItemNotFoundException()
  47. Else
  48. Return retVal
  49. End If
  50. End Get
  51. End Property
  52. #End Region
  53. #Region "Custom Exceptions"
  54. Public Class NetworkConnectionsFolderNotFoundException
  55. Inherits Exception
  56. Public Overrides ReadOnly Property Message() As String
  57. Get
  58. Return "The Network Connections Folder Could Not Be Found!"
  59. End Get
  60. End Property
  61. End Class
  62. Public Class LocalAreaConnectionFolderItemNotFoundException
  63. Inherits Exception
  64. Public Overrides ReadOnly Property Message() As String
  65. Get
  66. Return "The Local Area Connection Folder Could Not Be Found!"
  67. End Get
  68. End Property
  69. End Class
  70. #End Region
  71. End Class

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

  1. Module Module1
  2. Dim MainMenu As New Form1()
  3. Sub Main()
  4. MainMenu.Show()
  5. End Sub
  6. End Module

Once again, any help would be great!

Thanks,

JackHadding

1 comment:

  1. Sorry I can't be of any help... I failed my VB programming class... twice >_< #

    I do have a VB book that's like three inches thick though if you're interested. Remind me next time we talk & I'll give you more specifics if you want. Let me know. Hope you have an awesome time camping!!! *hugs*

    ReplyDelete