下面是一个例子,楼主拿去参考一下
以下是FORM1的全部代码,运行此示例,需要你在项目中引用COM——Ms Shell32(在system32文件夹下的shell32.dll)
程序代码:
Imports System.Runtime.InteropServices.GCHandle
Imports System.Runtime.InteropServices.GCHandleType
Public Class Form1
Private Const NetConnect As Integer = &H31
Private Function ExcNetLinkMenu(ByVal AdapterName As String, ByVal MenuName As String) As Boolean
On Error Resume Next
Dim Shell32 As Object
Dim lNameLenPtr As Integer
Dim lNameLen As Integer
Dim Handle As System.Runtime.InteropServices.GCHandle
Dim GCHandle As System.Runtime.InteropServices.GCHandleType
Handle = System.Runtime.InteropServices.GCHandle.Alloc("NetConnection", Pinned)
lNameLenPtr = Handle.AddrOfPinnedObject.ToInt32
Dim mShell As Shell32.Shell = New Shell32.Shell
Dim NetConnection As Shell32.Folder
Dim FolderItem As Shell32.FolderItem
Dim NetConnectionItem As New Shell32.ShellFolderItem
Dim verb As Shell32.FolderItemVerb
NetConnection = mShell.NameSpace(49)
If lNameLenPtr = 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 FolderItem
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 verb
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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'把 本地连接换成你要控制的本地连接的名字
Dim blnRelust As Boolean
blnRelust = ExcNetLinkMenu("本地连接", "停用(&B)")
If blnRelust Then
Console.Write("停用成功")
Else
blnRelust = ExcNetLinkMenu("本地连接", "禁用(&B)")
End If
If blnRelust Then
Console.Write("停用成功")
Else
Console.Write("停用失败")
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim blnRelust As Boolean
blnRelust = ExcNetLinkMenu("本地连接", "启用(&A)")
If blnRelust Then
Console.Write("启用成功")
Else
Console.Write("启用失败")
End If
End Sub
End Class
===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================