VB的Internet连接错误 [英] Internet connection error with VB
问题描述
您好,
我有此代码启用和禁用互联网访问:
Hello,
I have this code to enable and disable internet access:
Private Sub Button1_Click() Handles Button1.Click
Dim objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Dim colAdapters = objWMIService.Execquery("Select * from Win32_NetworkAdapter Where NetEnabled=True")
For Each adapter In colAdapters
MsgBox(adapter.DeviceId & " " & adapter.Name)
Next
Dim errReturn = colAdapters.ItemIndex(0).Enable()
If errReturn <> 0 Then
MsgBox("Enable Network adapter failed for adapter= " & colAdapters.ItemIndex(0).DeviceId)
Else
MsgBox("Enable Network adapter succeeded for adapter= " & colAdapters.ItemIndex(0).DeviceId)
End If
MsgBox("NetEnabled= " & colAdapters.ItemIndex(0).NetEnabled)
End Sub
Private Sub Button2_Click() Handles Button2.Click
Dim objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Dim colAdapters = objWMIService.Execquery("Select * from Win32_NetworkAdapter Where NetEnabled=True")
For Each adapter In colAdapters
MsgBox(adapter.DeviceId & " " & adapter.Name & vbCrLf)
Next
Dim errReturn = colAdapters.ItemIndex(0).Disable()
If errReturn <> 0 Then
msgbox("Enable Network adapter failed for adapter= " & colAdapters.ItemIndex(0).DeviceId & vbCrLf)
Else
MsgBox("Enable Network adapter succeeded for adapter= " & colAdapters.ItemIndex(0).DeviceId & vbCrLf)
End If
MsgBox("NetEnabled= " & colAdapters.ItemIndex(0).NetEnabled & vbCrLf)
End Sub
您可能想知道的事情:
*当我点击button2时,我的互联网断开连接,这是成功的。
*但是当我点击button1时,我的网络将不会重新连接并在线路上出错:
Dim errReturn = colAdapters。 ItemIndex(0).Enable()
*我的IDE突出显示Enable()为错误,并显示无效参数。
*我必须运行此程序作为管理员,因为没有其他工作否则
有谁知道如何解决这个问题?
Things you may want to know:
* When I click button2, my internet gets disconnected, so that's a success.
* But when I click button1, my internet will not reconnect and gives an error on the line:
Dim errReturn = colAdapters.ItemIndex(0).Enable()
* My IDE highlights the Enable() as being the error and it says "Invalid parameter".
* I have to run this program as an administrator as nothing works otherwise
Does anyone know how to fix this?
推荐答案
有你的您是否在两种情况下使用相同的查询条件?
您应该尝试仅启用未启用的内容(... Where NetEnabled = False
)。请在此处查看NetEnabled属性: http://msdn.microsoft.com/en -us / library / aa394216(v = vs.85).aspx [ ^ ]。我知道您使用了MSDN中的代码( http:// msdn。 microsoft.com/en-us/library/aa390385(v=vs.85).aspx [ ^ ]),但我认为这是错误的。
请注意,系统有多个适配器。使用 WmiExplorer [ ^ ]例如检查所有适配器状态。您应该在某个地方存储您禁用的设备的ID,以了解要启用的设备。
Have you seen that you are using the same query condition in both cases?
You should try to enable only what is not enabled ("...Where NetEnabled=False
"). See the NetEnabled property here: http://msdn.microsoft.com/en-us/library/aa394216(v=vs.85).aspx[^]. I know that you have used the code from MSDN (http://msdn.microsoft.com/en-us/library/aa390385(v=vs.85).aspx[^]), but I think it is wrong.
Be aware, that the system has several adapters. Use WmiExplorer[^] for example to check all adapter status. You should store somewhere the id of the device you disable to know which to enable.
这篇关于VB的Internet连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!