VB:如何启用/禁用本地连接?

其实没什么说的,无非就是先找到“网络连接”这个虚拟文件夹,然后找到要控制的本地连接对应的folderitem,然后枚举verb,找到需要的verb后,调用verb的DoIt方法,在winxp sp2 ,vb6 sp6下测试通过,代码如下:

Option Explicit

'首先引用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

Set 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

Set 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:

Set mShell = Nothing

Set NetConnection = Nothing

Set FolderItem = Nothing

Set NetConnectionItem = Nothing

Set verb = Nothing

End Function

Private Sub Command1_Click()

Dim flag As Boolean

'把 本地连接 2 换成你要控制的本地连接的名字

flag = ExcNetLinkMenu("本地连接 2", "停用(&B)") '这个在2000下对应的是禁用,具体是什么,点右键,自己看吧

End Sub

Private Sub Command2_Click()

'把 本地连接 2 换成你要控制的本地连接的名字

Dim flag As Boolean

flag = ExcNetLinkMenu("本地连接 2", "启用(&A)")

End Sub