在vb.net中实现断开和连接网络连接的代码,我在网上找了一段代码
Option Explicit On
'首先引用Microsoft Shell Controls And Automation
Private Function ExcNetLinkMenu(ByVal AdapterName As String, ByVal MenuName As String) As Boolean
On Error Resume Next
Dim mShell As New Shell32.Shell
Dim NetConnection As Shell32.Folder
Dim FolderItem As Shell32.FolderItem
Dim NetConnectionItem As ShellFolderItem
Dim verb As Shell32.FolderItemVerb
NetConnection = mShell.NameSpace(49) '这个49是我找出来的,有了它就可以避免遍历控制面板
If ObjPtr(NetConnection) = 0 Then
ExcNetLinkMenu = False
GoTo exitfunction
End If
Dim flag As Boolean
flag = False
For Each FolderItem In NetConnection.Items
If FolderItem.Name = AdapterName Then
NetConnectionItem = FolderItem
flag = True
Exit For
End If
Next
If flag = False Then
ExcNetLinkMenu = False
GoTo exitfunction
End If
For Each verb In NetConnectionItem.Verbs
If verb.Name = MenuName Then
flag = True
verb.DoIt()
ExcNetLinkMenu = True
GoTo exitfunction
End If
Next
If flag = False Then
ExcNetLinkMenu = False
GoTo exitfunction
End If
exitfunction:
mShell = Nothing
NetConnection = Nothing
FolderItem = Nothing
NetConnectionItem = Nothing
verb = Nothing
End Function
Private Sub Button1_Click()
Dim flag As Boolean
'把 本地连接 2 换成你要控制的本地连接的名字
flag = ExcNetLinkMenu("本地连接 2", "停用(&B)") '这个在2000下对应的是禁用,具体是什么,点右键,自己看吧
End Sub
Private Sub Button1_Click()
'把 本地连接 2 换成你要控制的本地连接的名字
Dim flag As Boolean
flag = ExcNetLinkMenu("本地连接 2", "启用(&A)")
End Sub
这是用vb写的代码,我在vb.net2005中的操作中提示
这条语句提示错误 1 语句在命名空间中无效。 C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 4 1 WindowsApplication1
该怎么改一下呀?在vb.net中要引入一个什么控件呀?
我是一个刚入门的,不知道该怎么改?哪位大侠帮忙给讲一下该怎么改呀?
或者在vb.net中的代码是什么呀?里面要放入什么控件,和要引入什么控件,希望给讲的详细一点,我是个菜鸟,谢谢了!!!分数不是问题
------解决方案--------------------
- VB.NET code
Imports Shell32
引用--》COM--》Microsoft Shell Controls And Automation
Public Class Form1
'首先引用Microsoft Shell Controls And Automation
Private Function ExcNetLinkMenu(ByVal AdapterName As String, ByVal MenuName As String) As Boolean
On Error Resume Next
Dim mShell As New Shell32.Shell
Dim NetConnection As Shell32.Folder
Dim FolderItem As Shell32.FolderItem
Dim NetConnectionItem As ShellFolderItem
Dim verb As Shell32.FolderItemVerb
NetConnection = mShell.NameSpace(49) '这个49是我找出来的,有了它就可以避免遍历控制面板
If NetConnection Is Nothing Or NetConnection.Items.Count = 0 Then
ExcNetLinkMenu = False
GoTo exitfunction
End If
Dim flag As Boolean
flag = False
For Each FolderItem In NetConnection.Items
If FolderItem.Name = AdapterName Then
NetConnectionItem = FolderItem
flag = True