How to disable/enable network connection in c#

后端 未结 7 1583
孤街浪徒
孤街浪徒 2020-11-30 05:55

Basically I\'m running some performance tests and don\'t want the external network to be the drag factor. I\'m looking into ways of disabling network LAN. What is an effecti

相关标签:
7条回答
  • 2020-11-30 06:32

    In VB.Net , You can also use it for toggle Local Area Connection

    Note: myself use it in Windows XP, it's work here properly. but in windows 7 it's not work properly.

      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 Sub
    
    0 讨论(0)
提交回复
热议问题