基本上,我正在运行一些性能测试,不希望外部网络成为拖累因素。我正在研究禁用网络局域网的方法。以编程方式完成此操作的有效方法是什么?我对c#很感兴趣。如果任何人有一个代码片段可以让你明白这一点,那将是很酷的。
发布于 2013-09-12 18:54:44
在VB.Net中,您还可以使用它来切换本地连接
注意:我自己在Windows XP中使用它,它在这里可以正常工作。但在windows7中,它不能正常工作。
Private Sub ToggleNetworkConnection()
Try
Const ssfCONTROLS = 3
Dim sConnectionName = "Local Area Connection"
Dim sEnableVerb = "En&able"
Dim sDisableVerb = "Disa&ble"
Dim shellApp = CreateObject("shell.application")
Dim WshShell = CreateObject("Wscript.Shell")
Dim oControlPanel = shellApp.Namespace(ssfCONTROLS)
Dim oNetConnections = Nothing
For Each folderitem In oControlPanel.items
If folderitem.name = "Network Connections" Then
oNetConnections = folderitem.getfolder : Exit For
End If
Next
If oNetConnections Is Nothing Then
MsgBox("Couldn't find 'Network and Dial-up Connections' folder")
WshShell.quit()
End If
Dim oLanConnection = Nothing
For Each folderitem In oNetConnections.items
If LCase(folderitem.name) = LCase(sConnectionName) Then
oLanConnection = folderitem : Exit For
End If
Next
If oLanConnection Is Nothing Then
MsgBox("Couldn't find '" & sConnectionName & "' item")
WshShell.quit()
End If
Dim bEnabled = True
Dim oEnableVerb = Nothing
Dim oDisableVerb = Nothing
Dim s = "Verbs: " & vbCrLf
For Each verb In oLanConnection.verbs
s = s & vbCrLf & verb.name
If verb.name = sEnableVerb Then
oEnableVerb = verb
bEnabled = False
End If
If verb.name = sDisableVerb Then
oDisableVerb = verb
End If
Next
If bEnabled Then
oDisableVerb.DoIt()
Else
oEnableVerb.DoIt()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Subhttps://stackoverflow.com/questions/172875
复制相似问题